tarnfeld
tarnfeld

Reputation: 26556

How to round up a number to nearest 10?

How can we round off a number to the nearest 10 in php?

Say I have 23, what code would I use to round it off to 30?

Upvotes: 113

Views: 140023

Answers (17)

JSowa
JSowa

Reputation: 10572

Update PHP 8.4

Since PHP 8.4 you have additional rounding modes that allow you to use the equivalent of ceil() and floor() functions with the precision parameter.

// round() with `floor` like rounding mode
round(23, -1, \RoundingMode::NegativeInfinity); // 20
round(27, -1, \RoundingMode::NegativeInfinity); // 20
    
// round() with `ceil` like rounding mode
round(23, -1, \RoundingMode::PositiveInfinity); // 30
round(27, -1, \RoundingMode::PositiveInfinity); // 30

Upvotes: 1

Hashan
Hashan

Reputation: 180

to nearest 10 , should be as below

$number = ceil($input * 0.1)/0.1 ;

Upvotes: 3

The Bad Brad
The Bad Brad

Reputation: 11

This can be easily accomplished using PHP 'fmod' function. The code below is specific to 10 but you can change it to any number.

$num=97;
$r=fmod($num,10);
$r=10-$r;
$r=$num+$r;
return $r;

OUTPUT: 100

Upvotes: 0

Rifat
Rifat

Reputation: 157

Hey i modify Kenny answer and custom it not always round function now it can be ceil and floor function

function roundToTheNearestAnything($value, $roundTo,$type='round')
    {
        $mod = $value%$roundTo;
        if($type=='round'){
            return $value+($mod<($roundTo/2)?-$mod:$roundTo-$mod);
        }elseif($type=='floor'){
            return $value+($mod<($roundTo/2)?-$mod:-$mod);
        }elseif($type=='ceil'){
            return $value+($mod<($roundTo/2)?$roundTo-$mod:$roundTo-$mod);
        }

    }

echo roundToTheNearestAnything(1872,25,'floor'); // 1850<br>
echo roundToTheNearestAnything(1872,25,'ceil'); // 1875<br>
echo roundToTheNearestAnything(1872,25,'round'); // 1875

Upvotes: 0

Robert
Robert

Reputation: 137

I wanted to round up to the next number in the largest digits place (is there a name for that?), so I made the following function (in php):

//Get the max value to use in a graph scale axis, 
//given the max value in the graph
function getMaxScale($maxVal) {
    $maxInt = ceil($maxVal);
    $numDigits = strlen((string)$maxInt)-1; //this makes 2150->3000 instead of 10000
    $dividend = pow(10,$numDigits);
    $maxScale= ceil($maxInt/ $dividend) * $dividend;
    return $maxScale;
}

Upvotes: 2

Nagibaba
Nagibaba

Reputation: 5348

For people who want to do it with raw SQL, without using php, java, python etc. SET SQL_SAFE_UPDATES = 0; UPDATE db.table SET value=ceil(value/10)*10 where value not like '%0';

Upvotes: 0

MAYOBYO HASSAN
MAYOBYO HASSAN

Reputation: 506

Try this......pass in the number to be rounded off and it will round off to the nearest tenth.hope it helps....

round($num, 1);

Upvotes: -2

Kenny
Kenny

Reputation: 236

I was actually searching for a function that could round to the nearest variable, and this page kept coming up in my searches. So when I finally ended up writing the function myself, I thought I would post it here for others to find.

The function will round to the nearest variable:

function roundToTheNearestAnything($value, $roundTo)
{
    $mod = $value%$roundTo;
    return $value+($mod<($roundTo/2)?-$mod:$roundTo-$mod);
}

This code:

echo roundToTheNearestAnything(1234, 10).'<br>';
echo roundToTheNearestAnything(1234, 5).'<br>';
echo roundToTheNearestAnything(1234, 15).'<br>';
echo roundToTheNearestAnything(1234, 167).'<br>';

Will output:

1230
1235
1230
1169

Upvotes: 22

Timo002
Timo002

Reputation: 3208

There are many anwers in this question, probably all will give you the answer you are looking for. But as @TallGreenTree mentions, there is a function for this.

But the problem of the answer of @TallGreenTree is that it doesn't round up, it rounds to the nearest 10. To solve this, add +5 to your number in order to round up. If you want to round down, do -5.

So in code:

round($num + 5, -1);

You can't use the round mode for rounding up, because that only rounds up fractions and not whole numbers.

If you want to round up to the nearest 100, you shoud use +50.

Upvotes: 13

tarnfeld
tarnfeld

Reputation: 26556

Try this:

ceil($roundee / 10) * 10;

Upvotes: 1

Daren Schwenke
Daren Schwenke

Reputation: 5478

floor() will go down.

ceil() will go up.

round() will go to nearest by default.

Divide by 10, do the ceil, then multiply by 10 to reduce the significant digits.

$number = ceil($input / 10) * 10;

Edit: I've been doing it this way for so long.. but TallGreenTree's answer is cleaner.

Upvotes: 255

wormhit
wormhit

Reputation: 3827

$value = 23;
$rounded_value = $value - ($value % 10 - 10);
//$rounded_value is now 30

Upvotes: 2

Adam Wright
Adam Wright

Reputation: 49376

We can "cheat" via round with

$rounded = round($roundee / 10) * 10;

We can also avoid going through floating point division with

function roundToTen($roundee)
{
  $r = $roundee % 10;
  return ($r <= 5) : $roundee - $r : $roundee + (10 - $r);
}

Edit: I didn't know (and it's not well documented on the site) that round now supports "negative" precision, so you can more easily use

$round = round($roundee, -1);

Edit again: If you always want to round up, you can try

function roundUpToTen($roundee)
{
  $r = $roundee % 10;
  if ($r == 0)
    return $roundee;
  return $roundee + 10 - $r;    
}

Upvotes: 3

Berry
Berry

Reputation: 1739

My first impulse was to google for "php math" and I discovered that there's a core math library function called "round()" that likely is what you want.

Upvotes: 0

Artyom Sokolov
Artyom Sokolov

Reputation: 2405

Try

round(23, -1);

Upvotes: 2

Will Barrett
Will Barrett

Reputation: 2647

round($number, -1);

This will round $number to the nearest 10. You can also pass a third variable if necessary to change the rounding mode.

More info here: http://php.net/manual/en/function.round.php

Upvotes: 180

Johnno Nolan
Johnno Nolan

Reputation: 29659

div by 10 then use ceil then mult by 10

http://php.net/manual/en/function.ceil.php

Upvotes: 7

Related Questions