Ace
Ace

Reputation: 4443

How do I round a number in JavaScript?

While working on a project, I came across a JS-script created by a former employee that basically creates a report in the form of

Name : Value
Name2 : Value2

etc.

The peoblem is that the values can sometimes be floats (with different precision), integers, or even in the form 2.20011E+17. What I want to output are pure integers. I don't know a lot of JavaScript, though. How would I go about writing a method that takes these sometimes-floats and makes them integers?

Upvotes: 67

Views: 104403

Answers (8)

Morizzla
Morizzla

Reputation: 21

You can also use the value.toFixed(n) function.

It will round the value to next value with 'n' decimals.

Upvotes: 0

Praveen
Praveen

Reputation: 186

Math.floor(19.5) = 19 should also work.

Upvotes: -1

user2768720
user2768720

Reputation: 49

A very good approximation for rounding:

function Rounding (number, precision){

var newNumber;
var sNumber = number.toString();

var increase = precision + sNumber.length - sNumber.indexOf('.') + 1;

if (number < 0)
  newNumber = (number -  5 * Math.pow(10,-increase));
else
  newNumber = (number +  5 * Math.pow(10,-increase));

var multiple = Math.pow(10,precision);

return Math.round(newNumber * multiple)/multiple;
}

Only in some cases when the length of the decimal part of the number is very long will it be incorrect.

Upvotes: 0

irfandar
irfandar

Reputation: 1810

You can also use toFixed(x) or toPrecision(x) where x is the number of digits.

Both these methods are supported in all major browsers

Upvotes: 20

Raj Rao
Raj Rao

Reputation: 9148

If you need to round to a certain number of digits use the following function

function roundNumber(number, digits) {
            var multiple = Math.pow(10, digits);
            var rndedNum = Math.round(number * multiple) / multiple;
            return rndedNum;
        }

Upvotes: 103

aemkei
aemkei

Reputation: 11215

You hav to convert your input into a number and then round them:

function toInteger(number){ 
  return Math.round(  // round to nearest integer
    Number(number)    // type cast your input
  ); 
};

Or as a one liner:

function toInt(n){ return Math.round(Number(n)); };

Testing with different values:

toInteger(2.5);           // 3
toInteger(1000);          // 1000
toInteger("12345.12345"); // 12345
toInteger("2.20011E+17"); // 220011000000000000

Upvotes: 52

Aron Rotteveel
Aron Rotteveel

Reputation: 83213

You can use Math.round() for rounding numbers to the nearest integer.

Math.round(532.24) => 532

Also, you can use parseInt() and parseFloat() to cast a variable to a certain type, in this case integer and floating point.

Upvotes: 16

Pablo Cabrera
Pablo Cabrera

Reputation: 5849

According to the ECMAScript specification, numbers in JavaScript are represented only by the double-precision 64-bit format IEEE 754. Hence there is not really an integer type in JavaScript.

Regarding the rounding of these numbers, there are a number of ways you can achieve this. The Math object gives us three rounding methods wich we can use:

The Math.round() is most commonly used, it returns the value rounded to the nearest integer. Then there is the Math.floor() wich returns the largest integer less than or equal to a number. Lastly we have the Math.ceil() function that returns the smallest integer greater than or equal to a number.

There is also the toFixed() that returns a string representing the number using fixed-point notation.

Ps.: There is no 2nd argument in the Math.round() method. The toFixed() is not IE specific, its within the ECMAScript specification aswell

Upvotes: 30

Related Questions