round number in JavaScript to N decimal places
Posted
by Richard
on Stack Overflow
See other posts from Stack Overflow
or by Richard
Published on 2010-02-08T11:19:56Z
Indexed on
2010/05/25
23:41 UTC
Read the original article
Hit count: 268
in JavaScript, the typical way to round a number to N decimal places is something like:
function round_number(num, dec) {
return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
}
However this approach will round to a maximum of N decimal places while I want to always round to N decimal places. For example "2.0" would be rounded to "2".
Any ideas?
© Stack Overflow or respective owner