简体   繁体   English

PHP-number_format和舍入

[英]PHP - number_format and rounding

The number_format function in PHP seems to round by default. PHP中的number_format函数似乎默认为四舍五入。 My understanding is that this function is useful for separating every three numbers with a comma. 我的理解是,此函数对于用逗号分隔每三个数字很有用。 Like 1403423 becomes 1,404,423 when using number_format. 就像1403423在使用number_format时变为1,404,423。

So, if I have a large number that I want rounded to two decimal places, how can I do this and still have the commas properly displayed? 因此,如果我想将大量数字四舍五入到小数点后两位,我该怎么做却仍然正确显示逗号?

Desired behavior: 12042.529 --> 12,042.53 期望的行为:12042.529-> 12,042.53

$a=12042.529;
echo number_format($a,2,'.',',');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM