Round money to nearest 10 dollars in Javascript

Marko picture Marko · Aug 12, 2010 · Viewed 21.3k times · Source

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

Answer

Toby picture Toby · Aug 12, 2010

Try

Math.round(val / 10) * 10;