Welcome to WebmasterWorld Guest from 23.23.36.47 register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts, Become a Pro Member
 Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

# PHP Server Side Scripting Forum

 Tweet
Mathmatical Question
Rounding Numbers Down and Up
stuart

Msg#: 193 posted 8:50 pm on May 13, 2003 (gmt 0)

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

Msg#: 193 posted 9:05 pm on May 13, 2003 (gmt 0)

Use [php.net...]

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

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

mischief

Msg#: 193 posted 5:56 am on May 14, 2003 (gmt 0)

42!

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

[php.net ]
[php.net ]

ShawnR

Msg#: 193 posted 7:42 am on May 14, 2003 (gmt 0)

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

Msg#: 193 posted 12:02 pm on May 14, 2003 (gmt 0)

Many thanks for your replies all. Much appreciated.

 Global Options: top home search open messages active posts  Tweet

 Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting