Page 1 of 1

Rounding

Posted: Tue Jun 03, 2008 7:19 am
by Bon Bon
I do not know if I am being thick here but I am pretty sure there is an easy way of rounding the last 3 digits of a number based on the value of the last 4 numbers.

For example:
100023 rounds to 100500
100499 rounds to 100500
100500 rounds to 101000
100501 rounds to 101000
100523 rounds to 101000

I did think about using a substr on the last 3 numbers or something and then using round but I was hoping for something a bit more advanced but easier to read. If anybody could help there I would appreciate it.

Re: Rounding

Posted: Tue Jun 03, 2008 7:23 am
by Chris Corbyn

Code: Select all

$number = 100563;
 
echo round($number / 1000) * 1000;
EDIT | In your case actually you want to replace 1000 with 100.
EDIT 2 | :lol: sorry I was right the first time...

Re: Rounding

Posted: Tue Jun 03, 2008 7:24 am
by Bon Bon
I was never good with math, spot on.

*Edit*
I used the following code, if anyone can do better please post a better solution, but if not please use the following for a reference:

Code: Select all

$price = substr($price, -3) > 500 ? round($price/1000)*1000 : round($price/1000)*1000 + 500;