PHP Round Up to Nearest Hundred Function

Here is a PHP Quickie!

NEW:

function roundNearestHundredUp($number)
{
return ceil( $number / 100 ) * 100;
}

OLD:

function roundNearestHundredUp($number){
$round = round($number, -2);
if($number > $round){ $round = $round + 100;}
return $round;
}

 

2 Responses to “PHP Round Up to Nearest Hundred Function”

  1. Dan says:

    You can achieve the same result in one line:

    function roundNearestHundredUp($number)
    {
    return ceil( $number / 100 ) * 100;
    }

  2. eligeske says:

    Thanks Dan! Nice way to NOT over complicate things. I am updating the post right now.

    changing:
    function roundNearestHundredUp($number){
    $round = round($number, -2);
    if($number > $round){ $round = $round + 100;}
    return $round;
    }

    To:
    function roundNearestHundredUp($number)
    {
    return ceil( $number / 100 ) * 100;
    }

Leave a Reply