round number in JavaScript to N decimal places
- by Richard
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?