在 php 中用 2 位小数格式化舍入值

formatting round value with 2 decimal in php

必须四舍五入 php 中的数字。 我试过如下,

round(12.4569,2);

它将 return 12.46。没问题。

但是, 回合(12.4003,2);

输出为 12.4

但需要输出12.40

请多多指教...

使用number_format:

number_format((float)$number, 2, '.', '');

使用sprintf函数的解决方案:

print_r(sprintf("%.2f",12.4003,2));   // 12.40
print_r(sprintf("%.2f",12.4569,2));   // 12.46

http://php.net/manual/en/function.sprintf.php

尝试在你的代码中使用它:

number_format(12.4003,2);