Welcome to WebmasterWorld Guest from 54.162.105.241

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

# Mathmatical Question

## Rounding Numbers Down and Up

#### stuart

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

#### Junior Member

joined:Nov 3, 2000
posts:142

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

#### ruserious

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

#### Junior Member

joined:Oct 26, 2002
posts:173

Use [php.net...]

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

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

#### mischief

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

#### New User

joined:Feb 9, 2003
posts:28

42!

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

[php.net ]
[php.net ]

#### ShawnR

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

#### Senior Member

joined:Mar 27, 2003
posts:664

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

#### stuart

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

#### Junior Member

joined:Nov 3, 2000
posts:142