Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round money to nearest 10 dollars in Javascript

Tags:

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

like image 334
Marko Avatar asked Aug 12 '10 00:08

Marko


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 whole number in JavaScript?

round() The Math. round() function returns the value of a number rounded to the nearest integer.

How do you round to 2 decimal places in JavaScript?

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.


1 Answers

Try

Math.round(val / 10) * 10; 
like image 61
Toby Avatar answered Oct 21 '22 15:10

Toby