Skip to content
Advertisement

PHP Rounding Float

I’m working on a system where I need to round down to the nearest penny financial payments. Naively I thought I would multiply up by 100, take the floor and then divide back down. However the following example is misbehaving:

JavaScript

correctly shows:

JavaScript

but

JavaScript

unexpectedly shows:

JavaScript

I get the same problem using intval for example.

I suspect the multiplication is falling foul of floating point rounding. But if I can’t rely on multiplication, how can I do this? I always want to round down reliably, and I don’t need to take negative amounts into consideration.

To be clear, I want any fractional penny amounts to be stripped off:

JavaScript

Advertisement

Answer

Since you mention you only use this for displaying purposes, you could take the amount, turn it into a string and truncate anything past the second decimal. A regular expression could do the job:

JavaScript

This expression works with any number of decimals (including zero). How it works in detail:

  • d+ matches any number of digits
  • .{0,1} matches 0 or 1 literal dot
  • d{0,2} matches zero or two digits after the dot

You can run the following code to test it:

JavaScript

Also available as a live test in this fiddle.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement