开发者

how to remove the last two zero?

I have a function cal开发者_运维知识库led zen_get_products_discount_price_qty($new_products->fields['products_id']) which outputs this: $8.0000. I want to turn the $8.0000 into $8.00. How can I do this?


use number_format function


   $output = zen_get_products_discount_price_qty($new_products->fields['products_id']);
   $output =  substr($output,0,-2);


Try this:

$value = '$8.0000';
$value = str_replace('$', '', $value);
$value = number_format($value, 2);
echo $value;

http://at2.php.net/number_format

It's important to use number_format to get correctly values. The function substr() only delete the last two zeros. The function number_format() round the number.

number_format(8.1199, 2); // == 8.12 - correct
substr(8.1199, 0, -2); // == 8.11 - false!


first remove $ then use round() function.


printf('$%.02f', 8.0000); // $8.00


Or preg_replace :)

echo preg_replace("/^\$(\d+?)(?:.\d*)?$/", "\$$1.00", '$8.0000');

Output: $8.00


DON'T use any substr(), printf(), number_format() etc. solutions. They don't take care of currencies other than USD where there might be different display requirements. Use instead Zen Cart global $currencies object:

$currencies->format($my_price);

Check includes/classes/currencies.php for reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜