Rounding a Number in JavaScript: Math.round()

JavaScript FAQ | JavaScript Numbers FAQ  

Question: How do I round a number to n decimal places?

Answer: To round a number in JavaScript, use the Math.round method:

Math.round(X);           // round X to an integer
Math.round(10*X)/10;     // round X to tenths
Math.round(100*X)/100;   // round X to hundredths
Math.round(1000*X)/1000; // round X to thousandths
...
To convert a number to a string that represents your number with exactly n decimal places, use the toFixed method.

Copyright © 1999-2011, JavaScripter.net.