Rounding

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Rounding

Post by spacebiscuit »

Hi,

Using the round function I am rounding number to two decimal places. This works fine until the unit is a 0.

So for example: 10.10 rounds to 10.1

How can I ensure the unit remains if it is a 0.

Thanks in advance,

Rob.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Rounding

Post by pickle »

10.10 is exactly the same as 10.1, so rounding isn't changing the value. PHP removes superfluous trailing zeros. I'd leave the zero off for the purposes of doing math do it. When you want to display it, use number_format()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Rounding

Post by spacebiscuit »

Thanks - I am displying a price so the trailing 0 is required in this case. The number_format function works perfectly.

Rob.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Rounding

Post by pickle »

If it's a price, you can also use money_format().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply