number_format
默认情况下愚蠢地舍入数字。 有没有一种简单的方法来关闭舍入? 我正在使用随机生成的数字,我可以得到类似的东西......
42533 * .003 = 127.599 或
,42533 * .03 = 1275.99 或
,42533 * .3 = 12759.9
我需要number_format(或其他东西)以传统的美国格式(用逗号分隔千)而不是四舍五入小数来表示结果。
有什么想法吗?
number_format的第二个参数是数字中的小数位数。找出答案的最简单方法可能是将其视为字符串(根据此问题)。传统的美式格式是默认行为,因此无需指定剩余参数。
$num_decimals = strlen(substr(strrchr($number, "."), 1));
$formatted_number = number_format($number, $num_decimals);
如果有人对此感兴趣,我写了一个小函数来解决这个问题。
感谢上面的乔尔·欣茨,我想出了......
function number_format_with_decimals($value, $round_to){
//$value is the decimal number you want to show with number_format.
//$round_to is the deicimal place value you want to round to.
$round_to_decimals = strlen(substr(strrchr($value, "."), $round_to));
$ans = number_format($value, $round_to);
return $ans;
}
我尝试了这些解决方案,但最终我的答案是这样的:
$output=money_format('%!i', $value);
这为您提供了类似 3,234.90 而不是 3,234 或 3,234.9