'Round to nearest and least multiple of 10

So here is my situation. I have a couple of numbers where I want to round to the nearest and least multiple of 10.

For example values in between 51 to 59 should round to 50.

Input = 59 = >Respose = 50

Input = 51 => Respose = 50

I have tried

$number = round(53, -1);

which will gives be 50 which is correct which I want, but if I try with 56, it will give me 60. But here also, am expecting 50. Can somebody help me out?

Accepted answer (In case some one is reading the question)

floor($number / 10)*10

However ,it gives be decimal values which I round and changed to

$amount = (int)floor($amount / 10)*10;


Solution 1:[1]

Use floor instead; first dividing by 10 then multiplying the truncated result back up:

$number = floor($number / 10) * 10

Using solutions such as $number = round($number - 5, -1); can cause you problems with floating point edge cases. (Interestingly that's how early Java implementations did it, with disastrous results.)

Solution 2:[2]

Round down:

$x = floor($x/10) * 10;

Round up:

$x = ceil($x/10) * 10;

Round to closest (up or down):

$x = round($x/10) * 10;

Solution 3:[3]

You have to divide your number by 10, then floor it, then multiply by 10 :

<?php

function floor10($input) {
    $input = $input/10;
    $input = floor($input);
    $input = 10*$input;
    return $input;
}

echo floor10(51); // echoes 50
echo floor10(59); // echoes 50

Solution 4:[4]

use int to remove decimal points after devide and then mul by 10 (int)($num/10)*10

Solution 5:[5]

You don't need any function for this its simple math.

/* rating percentage to nearest 10 */
$rating_percentage=92; //if 92 output will be 90

$r=$rating_percentage%10; //get the remainder after dividing by number 10
$rating_percentage=$rating_percentage-$r; //deduct the remainder to make the number perfectly dividible by 10 
$r=$r>=5?10:0; //if remainder is greater or equal to 5 make it 10 else 0
$rating_percentage=$rating_percentage+$r;//add the updated remainder back to number


print_r("<pre>");
print_r($rating_percentage); //output 90 for 91-94 and 100 for 96-100
print_r("</pre>");

Solution 6:[6]

Here is the complete function, UP, DOWN or ROUND to any number

function roundNumber($num, $to, $dir = null)
{
    if ($dir === 'down') {
        return floor($num / $to) * $to;
    } else if ($dir === 'up') {
        return ceil($num / $to) * $to;
    } else {
        return round($num / $to) * $to;
    }
}

Examples

echo roundNumber(104, 5);
// 105

echo roundNumber(102, 5);
// 100

echo roundNumber(104, 5, 'up');
// 105

echo roundNumber(104, 5, 'down');
// 100


echo roundNumber(115, 10);
// 120

echo roundNumber(114, 10);
// 110

echo roundNumber(113, 10, 'up');
// 120

echo roundNumber(113, 10, 'down');
// 110

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bathsheba
Solution 2
Solution 3 roberto06
Solution 4 Tariq
Solution 5 The Bad Brad
Solution 6 Kalim ul Haq