Welcome to WebmasterWorld Guest from 54.159.165.175

Forum Moderators: coopster & jatar k

Mathmatical Question

Rounding Numbers Down and Up

   
8:50 pm on May 13, 2003 (gmt 0)

10+ Year Member



Hi, Does anyone know the maths to round down or up 2 numbers, to the nearest 0 or 5, to achieve a price range as follows:

ie: 131(1st number) to 174(2nd number) = 130 to 175
ie: 136(1st number) to 179(2nd number) = 135 to 180
...etc,

I was thinking along the following lines but someone said its best done mathematically:

if 0 = 0
if 5 = 5

1st number:
if last digit is 1, 2, 3, 4 = 0
if last digit is 6, 7, 8, 9, = 5

2nd number:
if last digit is 1, 2, 3, 4 = 5
if last digit is 6, 7, 8, 9, = 0

Does that make sense? If so, any ideas?
TIA
stuart

9:05 pm on May 13, 2003 (gmt 0)

10+ Year Member



Use [php.net...]

If its only for displaying use [php.net...]

sprintf("%.2f", $price);

5:56 am on May 14, 2003 (gmt 0)

10+ Year Member



42!

ruserious has answered you perfectly, but for future reference the ceil() and floor() functions might be of interest:

[php.net ]
[php.net ]

7:42 am on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I must be running slow today, or maybe I haven't had my coffee yet. I don't get how you can get sprintf do what you need. Same with round, ceil, or floor unless they are coupled with a division then multiplication again. I'd suggest the following:

first number: floor($lower_price/5) * 5
second number: ceil($upper_price/5) * 5

Shawn

12:02 pm on May 14, 2003 (gmt 0)

10+ Year Member



Many thanks for your replies all. Much appreciated.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month