Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a number divisible by five with Math.Round

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])?

like image 854
Badr Hari Avatar asked Dec 13 '11 20:12

Badr Hari


People also ask

What numbers can be divisible by 5?

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.

How do you round to the nearest 5 in math?

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 .

How do you round a number to the nearest 5 in Python?

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.

How do you find the next multiple of 5 from a number in Java?

double number = Math. round((len + 5)/ 10.0) * 10.0; java.


3 Answers

function roundDownToMultiple(number, multiple) {
    return number - (number % multiple);
}   

roundDownToMultiple(86, 5); // 85

roundDownToMultiple(89, 5); // 85

roundDownToMultiple(96, 5); // 95
like image 101
Michael Robinson Avatar answered Sep 28 '22 02:09

Michael Robinson


Divide by 5, round it, multiply by 5.

alert(Math.round(83 / 5) * 5);

jsFiddle Demo

like image 24
Alex Turpin Avatar answered Sep 28 '22 02:09

Alex Turpin


Using this Math.round(Math.floor(Math.random() * 100) / 5) * 5 You can get the Numbers divisible by 5.

  • 100 - is the range of the Result.
like image 38
Saravana Kumar Avatar answered Sep 28 '22 01:09

Saravana Kumar