I have a number variable that is between 0 and 100. It ccould be something like 83.333334.
I want to use Math.Round
to round the number (e.g. Math.round(83.333334);
). How can I do this so that the result is always divisible by five (i.e. in the set [0, 5, 10, 15... 85, 90, 95, 100])?
Divisibility Rule of 5If a number ends with 0 or 5, it is divisible by 5. For example, 35, 790, and 55 are all divisible by 5.
This is a two step process: Divide the number by 5 and round the result to the nearest integer. Multiply the result by 5 to get the number rounded to the nearest 5 .
To round a number up to the nearest 5:Call the math. ceil() method passing it the number divided by 5 . Multiply the result by 5 . The result of the calculation is the number rounded up to the nearest five.
double number = Math. round((len + 5)/ 10.0) * 10.0; java.
function roundDownToMultiple(number, multiple) {
return number - (number % multiple);
}
roundDownToMultiple(86, 5); // 85
roundDownToMultiple(89, 5); // 85
roundDownToMultiple(96, 5); // 95
Divide by 5, round it, multiply by 5.
alert(Math.round(83 / 5) * 5);
jsFiddle Demo
Using this Math.round(Math.floor(Math.random() * 100) / 5) * 5
You can get the Numbers divisible by 5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With