【发布时间】:2010-12-07 10:15:51
【问题描述】:
我需要在 2dp 中存储代表货币金额的数字,以便输出到屏幕上。相同的数字在格式化后可以用数学函数进行更改。我确定我记得过去由于数字格式的数字被视为字符串而出现错误,而千位分隔符是导致问题的原因,但下面的脚本运行时没有错误。
PHP 的松散数据类型是否可以节省时间?相对较新的 PHP 版本有什么变化吗?如果不是,在其他情况下number_format() 仍然会因为数字是字符串而导致数学函数失败?
echo round(1111123.2, 2); //included to demonstrate how it can't be used for this purpose
echo "<br />";
echo number_format(1111123.2, 2);
echo "<br />";
$num = number_format(1111123.2, 2);
echo number_format($num / 42, 2);
// output
1111123.2
1,111,123.20
0.02
【问题讨论】:
标签: php number-formatting