110

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?

mahemoff
  • 44,526
  • 36
  • 160
  • 222
tarnfeld
  • 25,992
  • 41
  • 111
  • 146

16 Answers16

250

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.

Daren Schwenke
  • 5,428
  • 3
  • 29
  • 34
  • 4
    TallGreenTree's answer does not always round it UP. This answer is the most correct with the use of ceil() – Brandon May 28 '16 at 21:30
  • 1
    TallGreenTree's answer does support rounding up/down using the third parameter (mode) of round(). round($input,-1, PHP_ROUND_HALF_UP) – Daren Schwenke Aug 31 '16 at 16:45
  • 5
    @DarenSchwenke, that third param doesn't quite work as you assume, that only controls the case of exactly halfway between the intervals. `(15,-1, PHP_ROUND_HALF_UP); // 20`, `(14,-1, PHP_ROUND_HALF_UP); // 10` – Arth Oct 03 '16 at 09:33
179
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

Will Barrett
  • 2,607
  • 1
  • 16
  • 18
  • 11
    Can you blame them for assuming the questioner meant "round to the nearest 10" when the question said "round to the nearest 10" twice? – ceejayoz Oct 26 '09 at 01:20
  • This answer was posted before the questioner clarified himself. I just figured he wasn't rounding correctly in the question. – Will Barrett Oct 26 '09 at 13:56
  • 12
    TallGreenTree is correct. You can set the rounding mode with the 3rd argument: "mode One of PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, or PHP_ROUND_HALF_ODD. " – d-_-b Jun 17 '11 at 02:40
  • 13
    This doesn't give the right outcome. `23` will be `20` and not `30` because it will always rond to the nearest 10. The rounding mode will not help, since this only round half, after the `x.`. – Timo002 Jun 16 '14 at 11:56
  • +1 for not using the classic `round( $x / $y ) * $y` solution. Avoiding the division and multiplication can save precious clock cycles – stevendesu Mar 15 '15 at 03:01
  • 1
    Sad that ceil() and floor() don't have the precision parameter yet. – squarecandy Apr 18 '15 at 00:20
20

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
Kenny
  • 216
  • 2
  • 2
  • 1
    Kenny, you are the best. I only wish I had a graphic representation of how it rounds exactly so I'm sure it gives the expected results in 100% of cases. I guess I'll have to do some testing. – Slava Oct 04 '16 at 11:10
  • Is there any way to modify this code to add a third parameter with round mode HALF_UP / HALF_DOWN? – Slava Oct 04 '16 at 11:21
  • Does not work for the number: 2560.9999999999995 – Raf A. Jan 21 '23 at 13:55
12

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.

Timo002
  • 3,138
  • 4
  • 40
  • 65
7

div by 10 then use ceil then mult by 10

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

Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
3

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;    
}
Adam Wright
  • 48,938
  • 12
  • 131
  • 152
3

to nearest 10 , should be as below

$number = ceil($input * 0.1)/0.1 ;
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
Hashan
  • 164
  • 8
  • Great answer. Much simpler than the other methods. If you want to round down to the nearest 10, just change `ceil` to `floor`. – Gavin May 08 '23 at 06:24
2

Try

round(23, -1);

Artyom Sokolov
  • 2,385
  • 3
  • 23
  • 34
2

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;
}
Robert
  • 137
  • 3
  • 17
2
$value = 23;
$rounded_value = $value - ($value % 10 - 10);
//$rounded_value is now 30
wormhit
  • 3,687
  • 37
  • 46
1

Try this:

ceil($roundee / 10) * 10;
Florent
  • 12,310
  • 10
  • 49
  • 58
tarnfeld
  • 25,992
  • 41
  • 111
  • 146
0

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.

Berry
  • 1,719
  • 2
  • 11
  • 15
0

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';

Nagibaba
  • 4,218
  • 1
  • 35
  • 43
0
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
Rifat
  • 147
  • 2
  • 4
0

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

-2

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);

MAYOBYO HASSAN
  • 467
  • 6
  • 10