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

Related Post

String Number conversion in JavaScript
jQuery – A lightweight JavaScript Library
Create a random number between 1 to 10 excluding 3, 5, and 9.?
You are given a list of n numbers from 1 to n-1, with one of the numbers repeated. Findout the repeated number?
Initializing Array in JavaScript

Comments

Leave a Reply




Technology