Today we discuss about the nearest Value in PHP.
Suppose that I have nmber like 456 its will become 460.
You can get right value using below method.
Nearest the value in PHP
echo roundToTheNearestAnything(121, 10).'<br>';
echo roundToTheNearestAnything(8, 5).'<br>';
echo roundToTheNearestAnything(1, 2).'<br>';
function roundToTheNearestAnything($value, $roundTo)
{
$mod = $value%$roundTo;
return $value+($mod<($roundTo/2)?-$mod:$roundTo-$mod);
}
Nearest the value in jQuery
function roundToTheNearestAnything(value, roundTo){
var mod = value%roundTo;
return value+(mod<(roundTo/2)?-mod:roundTo-mod);
}
var totalPart = 19;
if(totalPart >0 && totalPart <=2){
var valme = roundToTheNearestAnything(totalPart, 2);
}else if(totalPart >2 && totalPart <=9){
var valme = roundToTheNearestAnything(totalPart, 5);
}else{
var valme = roundToTheNearestAnything(totalPart, 10);
}
console.log(valme);
Related Post – Encrypt And Decrypt With Salt In PHP