Tuesday, April 5, 2011

php float problem

Example:
326.3 * 100 => 32630 (in double, as displayed in echo)
But, when you floor(326.3*100) => resulted as 32629 (sigh*)

The issue is because after multiplying 326.3 by 100, the double is kept as 32629.9999999 instead of 32630.

But if you floor(32630), as raw integer, you will get 32630.

To fix this issue, convert the result in string, and floor it.
Example:
$iResult = floor((326.3*100)."");
//resulting: 32630.


No comments: