Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript using round to the nearest 10 [closed]

Tags:

javascript

I would like to round integers using JavaScript. For example:

10 = 10 11 = 20 19 = 20 24 = 30 25 = 30 29 = 30 
like image 879
mark denfton Avatar asked Jun 13 '12 20:06

mark denfton


People also ask

How do you round to the nearest 10 in JavaScript?

round to Round a Number to the Nearest 10 in JavaScript. To round a number to the nearest 10 , you can call the Math. round() function, passing the number divided by 10 and multiplying the result by 10 , e.g., Math. round(num / 10) \* 10 .

How do you round to the nearest tenth?

Whenever you want to round a number to a particular digit, look only at the digit immediately to its right. For example, if you want to round to the nearest tenth, look to the right of the tenths place: This would be the hundredths place digit. Then, if it is 5 or higher, you get to add one to the tenths digit.

How do you round down in JavaScript?

The Math. floor() method rounds a number DOWN to the nearest integer.

How do you round the number 7.25 to the nearest integer in JavaScript?

The Math. round() function in JavaScript is used to round the number passed as parameter to its nearest integer. Parameters : The number to be rounded to its nearest integer.


Video Answer


2 Answers

This should do it:

Math.ceil(N / 10) * 10; 

Where N is one of your numbers.

like image 171
alexn Avatar answered Sep 29 '22 17:09

alexn


To round a number to the next greatest multiple of 10, add one to the number before getting the Math.ceil of a division by 10. Multiply the result by ten.

Math.ceil((n+1)/10)*10;

1->10 2->10 3->10 4->10 5->10 6->10 7->10 8->10 9->10 10->20 11->20 12->20 13->20 14->20 15->20 16->20 17->20 18->20 19->20 20->30 21->30 22->30 23->30 24->30 25->30 26->30 27->30 28->30 29->30 30->40 35-> 40 40-> 50 45-> 50 50-> 60 55-> 60 60-> 70 65-> 70 70-> 80 75-> 80 80-> 90 85-> 90 90-> 100 95-> 100 100-> 110 
like image 34
kennebec Avatar answered Sep 29 '22 16:09

kennebec