php round bug

If you use the round function in php, it might get tricky because of the following:

echo round(3.1234, 2);

Will print 3.12, but, for example if you have the following:

echo round(3.1000, 2);

Will print 3.1 instead of 3.10. Even if you use the round function with two decimals php will automatically correct the result because 3.1 = 3.10.

In order to get rid of this you will need to use the number_format function as in the example below:

echo number_format(round(3.1000, 2), 2, '.', ',');

This code will print 3.10 instead of 3.1.

From my point of view this is a clear bug because 3.1000 rounded with 2 decimal precision is not 3.1, it’s clearly 3.10.


Leave a Reply

Your email address will not be published. Required fields are marked *