PHP圆形意外结果

时间:2015-10-27 17:22:33

标签: php rounding

我有一个PHP圆函数的问题。 Rounds功能无法正常工作:

$value = 562.92578125;

round($value,1); // return 562.9 (correctly)
round($value,2); // return 562.9299999999999 (wrong)
round($value,3); // return 562.926 (correctly)

2 个答案:

答案 0 :(得分:0)

工作正常,

$value = 562.92578125;

round($value,1); // return 562.9
round($value,2); // return 562.93
round($value,3); // return 562.926

请参阅demo here

答案 1 :(得分:0)

作为解决方法尝试使用:

number_format(round($value, 1), 1);
number_format(round($value, 2), 2);
number_format(round($value, 3), 3);

另外,请看一下:Can I rely on PHP php.ini precision workaround for floating point issue