1. Aspiring Evangelist
Join Date
Aug 2003
Posts
424

## [PHP] Specific Rounding?

Hi there,

I'm familiar with the round function in PHP, but I need to run a rather specific round which goes way beyond my skills!

I'm finding it difficult to explain, so I guess an example would be best:

2.41 becomes 2.45
2.44 becomes 2.45
2.49 becomes 2.50
2.52 becomes 2.55

Basically, its always rounding to a square number, either a x.x5 or x.x0.
Oh, and I always wish for it to round up, never down!

I've searched and searched and it seems its too specific to find an example. When writing it myself, the closest I've got so far just rounds the very first digit.

Thank you in advance for any advice you guys can offer - I'm definitely finding this tricky!

Kindest Regards,

2. Web Hosting Master
Join Date
Mar 2004
Location
USA
Posts
4,342
PHP Code:
``` if(substr(\$number,-1,1) <= 4){  \$new_number = substr(\$number,0,strlen(\$number)-1).'5';} else {  \$new_number = substr(\$number,0,strlen(\$number)-1).'0';}  ```
Didn't try it thou.

Peace,

3. PHP for breakfast
Join Date
May 2004
Location
Lansing, MI, USA
Posts
1,548
Originally Posted by azizny
PHP Code:
``` if(substr(\$number,-1,1) <= 4){   \$new_number = substr(\$number,0,strlen(\$number)-1).'5'; } else {   \$new_number = substr(\$number,0,strlen(\$number)-1).'0'; }  ```
Didn't try it thou.

Peace,
I'd also recommend (though granted, this is 'if' the code works as proposed above) throwing in a: \$number = number_format(\$number, 2, '.', ''); at the top of that just to ensure proper formatting before sending it off.

4. Aspiring Evangelist
Join Date
Aug 2003
Posts
424
Originally Posted by azizny
PHP Code:
``` if(substr(\$number,-1,1) <= 4){  \$new_number = substr(\$number,0,strlen(\$number)-1).'5';} else {  \$new_number = substr(\$number,0,strlen(\$number)-1).'0';}  ```
Didn't try it thou.

Peace,
Thank you so much azizny! I've tried out the script and its almost there, however, for some reason if I enter a number such as 12.59 it rounds it down to 12.50. Also, is there any way of making this compatible with a number containing more than 2 decimal places? For example 12.58323 would round to 12.60.

I appreciate that asking you this may be above and beyond the generosity of a forum - if so please accept my apologies. Thank you again for all your help so far, it really is much appreciated!

Thank you also WO-Jacob for your suggestion - it's as good as done!

Kindest Regards,

5. PHP for breakfast
Join Date
May 2004
Location
Lansing, MI, USA
Posts
1,548
PHP Code:
``` function craftyRound(\$number = 0) {   \$number = round(\$number, 2);   \$check_digit = substr(\$number, -1);   \$whole = floor(\$number);   \$tens_decimal = substr(\$number, -2, 1);   if (\$check_digit == 0 || \$check_digit == 5)     return \$number;   if (\$check_digit > 0 && \$check_digit < 5)     return \$whole . '.' . \$tens_decimal . '5';   if (\$check_digit > 5 && \$check_digit < 10)     return number_format(round(\$number, 1), 1, '.', '').'0'; }  ```
I /believe/ this should do it, and also cover 4.99 rolling to 5.00 and such cases... I made it a little more 'lengthy' to help understanding as well.

6. Aspiring Evangelist
Join Date
Aug 2003
Posts
424
Originally Posted by WO-Jacob
PHP Code:
``` function craftyRound(\$number = 0) {  \$number = round(\$number, 2);  \$check_digit = substr(\$number, -1);  \$whole = floor(\$number);  \$tens_decimal = substr(\$number, -2, 1);  if (\$check_digit == 0 || \$check_digit == 5)    return \$number;  if (\$check_digit > 0 && \$check_digit < 5)    return \$whole . '.' . \$tens_decimal . '5';  if (\$check_digit > 5 && \$check_digit < 10)    return number_format(round(\$number, 1), 1, '.', '').'0';}  ```

I /believe/ this should do it, and also cover 4.99 rolling to 5.00 and such cases... I made it a little more 'lengthy' to help understanding as well.
Whoa, thank you so much! Works perfectly. Thank you also for making it a bit simpler - its always nice to learn instead of just using it.

Ideally I'd like to calculate these roundings inside a loop. I've defined the function at the top of the script, however when I refer to the function I get a 'cannot redeclare function' error. I've come across these in the past and have never understood a way round them - is it a simple fact that you can't refer to a function from inside a loop?

7. PHP for breakfast
Join Date
May 2004
Location
Lansing, MI, USA
Posts
1,548
Sorry, should have included usage...

you'd have the function at the top, then you'd do something like:

for (\$i = 0; \$i < 1; \$i = \$i + .01)
echo craftyRound(\$i);

Make sense?

8. Aspiring Evangelist
Join Date
Aug 2003
Posts
424
Originally Posted by WO-Jacob
Sorry, should have included usage...

you'd have the function at the top, then you'd do something like:

for (\$i = 0; \$i < 1; \$i = \$i + .01)
echo craftyRound(\$i);

Make sense?
I do normally know how to use a function - honest!

The problem here is I need to run the \$rounded = craftyRound(\$number) inside a loop. This is kinda unavoidable as the loop is calculating loads of other things to help determine this value. More accurately, I'm calculating \$rounded[\$number] in order for it to be called and refered to later on.

Does this make sense?

9. Web Hosting Master
Join Date
Apr 2003
Location
Los Angeles, CA
Posts
800
Don't make your life so difficult. This will round up to the nearest 20th, i.e. 0.05, and also format it so it always has two decimal places:

\$rounded = number_format(ceil(\$number*20)/20, 2);

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•