Rounding number in javascript up to some decimal point
If we want to round a number up to certain decimal point, in that case Math.round(x) won’t work . For example if x = 5.67, then Math.round(x) will be 6. In certain cases we want rounding up to some decimal places (y). To do that We need to do the following 1. Multiply given number by 10^y 2. Now round the result using Math.round. e.g. Math.round(x*10^y) 3. Divide number by 10^y. If x = 10.567 and we want to format x up to two points of decimal. We need to do the following
var result = Math.round(x * 100)/100;
And the output will be 10.57
May 30, 2008 | Filed Under Javascript
Related Post
Comments
Leave a Reply