Round a number to the nearest 10 using JavaScript
Question:
How to round a number to the nearest 10 using JavaScript? Answer:
const num1 = Math.round(11.95 / 10) * 10;
const num2 = Math.round(13.45 / 10) * 10;
const num3 = Math.round(18.05 / 10) * 10;
console.log(num1 + ' - ' + num2 + ' - ' + num3); // 10 - 10 - 20
Description:
To round a number to the nearest 10 you can use the pure JavaScript function Math.round() with an extra division and multiplication.
Reference:
Math.round reference
Share "How to round a number to the nearest 10 using JavaScript?"
Related snippets:
- Check if number is odd or even in JavaScript
- Round a number in JavaScript
- Get the absolute value of a number in JavaScript
- Generate random string in JavaScript
- Generate random date with JavaScript
- Generate a random number with fixed length using JavaScript
- Generate a random number in a range with JavaScript
- Get the minimum value in an array using JavaScript
- Get the maximum value in an array using JavaScript
- Convert a float number to integer in JavaScript
- Round a number to the nearest 10 using JavaScript
- Round down a number in JavaScript
- Round up a number in JavaScript
Tags:
round, round to 10, math Technical term:
Round a number to the nearest 10 using JavaScript