-2

I have a problem with php round function. Rounds function not working correctly:

$value = 562.92578125;

round($value,1); // return 562.9 (correctly)
round($value,2); // return 562.9299999999999 (wrong)
round($value,3); // return 562.926 (correctly)
  • 1
    Is this all of the code you are working with because the round function works properly? – Crinkley Crewms Oct 27 '15 at 17:26
  • I have found the problem, an included class (php excel) return me unexpected value from round function. An updated version of this class return a correctly result of round function. – Alessandro Romeo Oct 27 '15 at 18:11

2 Answers2

0

Its working fine,

$value = 562.92578125;

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

See demo here

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
0

As a workaround try to use:

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

Also, have a look here: Can I rely on PHP php.ini precision workaround for floating point issue

Community
  • 1
  • 1
Kostas Mitsarakis
  • 4,772
  • 3
  • 23
  • 37