[英]PHP - number_format issues
默认情况下, 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);
如果有人对此感兴趣,我已经写了一个小函数来解决这个问题。
归功于以上的Joel Hinz,我想出了...
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.