Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  79] [ 4]  / answers: 1 / hits: 95998  / 15 Years ago, thu, november 5, 2009, 12:00:00

Using Javascript, I would like to round a number passed by a user to the nearest 10. For example, if 7 is passed I should return 10, if 33 is passed I should return 30.


More From » javascript

 Answers
39

Divide the number by 10, round the result and multiply it with 10 again, for example:



  1. 33 / 10 = 3.3

  2. 3.3 rounded = 3

  3. 3 × 10 = 30




console.log(Math.round(prompt('Enter a number', 33) / 10) * 10);




[#98376] Monday, November 2, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maxinec

Total Points: 117
Total Questions: 116
Total Answers: 116

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
maxinec questions
;