homepage Welcome to WebmasterWorld Guest from 54.161.166.171
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Mathmatical Question
Rounding Numbers Down and Up
stuart




msg:1298566
 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:1298567
 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:1298568
 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:1298569
 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:1298570
 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  
 

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

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved