8 September 2010 8 Comments

Cut-N-Paste Corner: PHP function – Dynamically Round up to nearest 10, 100, 1000 or whatever (Graphing Axis Sex Toy)

One from the "Random-Shit-Pulled-Out-of-the-Junk-Drawer" Department... as I sit here and watch my beloved Red Sox cement their way OUT of playoffs... {sigh}

Here's a little function I use when building sexy little graphs in my PHP Reporting / Web Interfaces. Its good to calculate a MAX value for the graph axis. Since we never know what the values are going to be until we coax that data out of the DB - it could be 10, could be 10,000 or 10,000,000 (sometimes its even "apple", but fruit to integer conversions will be another day)

<?php

// $biggs would be the biggest num in your set
// (assuming a "max" value graph-building situation
function RoundUptoNearestN($biggs){

       $rounders = (strlen($biggs)-2) * -1;
       $places = strlen($biggs)-2;

       $counter = 0;
               while ($counter <= $places) {
                   $counter++;
                       if($counter==1) {
                               $holder = $holder.'1'; }
                       else {
                               $holder = $holder.'0'; }
               }

                       $biggs = $biggs + $holder;
                       $biggs = round($biggs, $rounders);

                       if($biggs<100) {
                                       if($biggs<100) { $biggs = 100; }
                                       else { $biggs = 100; }
                                                       }
       return $biggs;
}

//Usage:
echo '<pre>';
echo RoundUptoNearestN(1424)."\n"; //returns 1500
echo RoundUptoNearestN(123)."\n"; //returns 130
echo RoundUptoNearestN(154368)."\n"; //returns 160000
echo RoundUptoNearestN(12334)."\n"; //returns 13000
echo RoundUptoNearestN(75)."\n"; //returns 100
echo '</pre>';
///etc etc etc :P

?>

I'm sure there is a WAY more elegant way to do this using the CEIL function, but for my needs - this fit the bill and I couldn't find anything out there on the interwebs that worked exactly like I wanted for this purpose (aka "I never want to round down, and still need to keep it close to the original number").

If you were wondering about the "sexy little graph" thing I mentioned - check out Open Flash Chart, its quick, pretty and just-plain-works.

If anyone wants to correct me with a ONE LINE version to embarrass me - go for it! But remember:

  1. It doesn't mean that I'm wrong
  2. It won't add inches to your dick
  3. Those Mountain Dew Rumors might be true
  4. EVERYONE can see your bald spot - stop living a lie!

I keed! I keed! :)

  • http://t ghnmgh

    your wrong.

    • Ry

      Am I? Fix me then, Mr. Ghnmgh.

      Also, its “You’re wrong” as in “You ARE” – so in effect, your response is 50% wrong to begin with.

      • not ghnmgh

        Also, it’s “it’s” as in “it IS” not “its” as in “belongs to IT.”

        just sayin.

        • Ry

          Well played. :)

  • Filip Nohe

     if($biggs<100) {

                                           if($biggs<100) { $biggs = 100; }

                                           else { $biggs = 100; }

                                                           }

  • hhhooommmeee

    Why is that if I rounded 344 it says 360. It is far from 360 I guess. Why it is not 350? Thanks!

    • http://ryrobes.com/ Ryan Robitaille

      Let me check it out tomorrow. I wrote this a LONG time ago for a very specific use case. I’m sure it needs fixing. :)

  • Slityak

    I’m used this script to round prices in multiple currencies. Now, I’m changed it to the following:
    function RoundUptoNearestN($biggs){
    $exp = pow(10,floor(strlen($biggs)/2));
    return ceil( $biggs/ $exp ) * $exp;
    }
    It’s not completed yet, I think, but at least not rounding $11 to $100 :-D