我有一个名为的函数zen_get_products_discount_price_qty($new_products->fields['products_id'])
,它输出以下内容:$8.0000
. 我想把它$8.0000
变成$8.00
. 我怎样才能做到这一点?
问问题
1297 次
7 回答
2
使用number_format函数
于 2011-09-23T14:07:03.843 回答
2
$output = zen_get_products_discount_price_qty($new_products->fields['products_id']);
$output = substr($output,0,-2);
于 2011-09-23T14:12:15.077 回答
1
first remove $ then use round() function.
于 2011-09-23T14:09:02.410 回答
1
printf('$%.02f', 8.0000); // $8.00
于 2011-09-23T14:14:50.833 回答
1
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!
于 2011-09-23T14:12:12.503 回答
0
或 preg_replace :)
echo preg_replace("/^\$(\d+?)(?:.\d*)?$/", "\$$1.00", '$8.0000');
输出:8.00 美元
于 2011-09-23T14:13:33.657 回答
0
不要使用任何 substr()、printf()、number_format() 等解决方案。他们不处理可能有不同显示要求的美元以外的货币。改用 Zen Cart 全局 $currencies 对象:
$currencies->format($my_price);
检查 include/classes/currencies.php 以供参考。
于 2011-11-09T20:30:50.053 回答