How can I round a decimal number in Javascript to the nearest 10? My math is pretty rubbish today, it could be the 2 hour sleep :/
Some sample cases
$2823.66 = $2820 $142.11 = $140 $9.49 = $10
I understand I probably need a combination of Math.round/floor but I can't seem to get expected result.
Any help/pointers appreciated!
M
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 .
round() The Math. round() function returns the value of a number rounded to the nearest integer.
Use the toFixed() method to round a number to 2 decimal places, e.g. const result = num. toFixed(2) . The toFixed method will round and format the number to 2 decimal places.
Try
Math.round(val / 10) * 10;
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