JavaScript Introduction | Standard Objects | Number Methods

Number Methods

Number methods are methods defined on the Number object and used for number-related operations.
The most commonly used representative Number methods are as follows.

  1. Number.parseFloat()
  2. Number.parseInt()
  3. Number.isNaN()
  4. Number.isFinite()
  5. Number.isInteger()
  6. Number.isSafeInteger()

Number.parseFloat() Method

The Number.parseFloat() method parses a string and returns the numeric part contained in the string as a real number. If several numbers exist in the string, it returns only the first number as a real number.

This method behaves exactly the same as the global parseFloat() function.

Number.parseFloat("12");         // 12
Number.parseFloat("12.34");      // 12.34
Number.parseFloat("12string");   // 12
Number.parseFloat("12 34 56");   // 12
Number.parseFloat("string 56")); // NaN

This method is not supported in Safari or Internet Explorer.

Number.parseInt() Method

The Number.parseInt() method parses a string and returns the numeric part contained in the string as an integer.
If several numbers exist in the string, it returns only the first number as an integer.

This method behaves exactly the same as the global parseInt() function.

Number.parseInt("12");         // 12
Number.parseInt("12.34");      // 12
Number.parseInt("12string");   // 12
Number.parseInt("12 34 56");   // 12
Number.parseInt("string 56")); // NaN

This method is not supported in Safari or Internet Explorer.

Number.isNaN() Method

The Number.isNaN() method checks whether the passed value is NaN.

This method prevents the problems caused by forced conversion to numbers that occur with the global isNaN() function.
This method works only on numeric values and returns true only when that value is NaN.

Number.isNaN(NaN);       // true
Number.isNaN(0 / 0);     // true
// The following examples return incorrect results with the global isNaN() function.
isNaN("NaN");            // true
isNaN(undefined);        // true
isNaN("string");         // true
// Number.isNaN() returns the correct results.
Number.isNaN("NaN");     // false
Number.isNaN(undefined); // false
Number.isNaN("string");  // false

This method is not supported in Internet Explorer.

Number.isFinite() Method

The Number.isFinite() method checks whether the passed value is a finite number.

Unlike the global isFinite() function, this method does not forcibly convert the passed value to a number.
This method works only on countable values and returns true only when that value is finite.

Number.isFinite(0);        // true
Number.isFinite(3e45);     // true
Number.isFinite(Infinity); // false
Number.isFinite(NaN);      // false
// The following examples return incorrect results with the global isFinite() function.
isFinite("0");             // true
isFinite(null);            // true
// Number.isFinite() returns the correct results.
Number.isFinite("0");      // false
Number.isFinite(null);     // false

This method is not supported in Internet Explorer.

Number.isInteger() Method

The Number.isInteger() method checks whether the passed value is an integer.
It returns true if the passed value is an integer, and returns false for values that are not integers or for values such as NaN and Infinity.

Number.isInteger(0);        // true
Number.isInteger(-100);     // true
Number.isInteger(0.1);      // false
Number.isInteger("string"); // false
Number.isInteger(Infinity); // false
Number.isInteger(true);     // false

This method is not supported in Safari or Internet Explorer.

Number.isSafeInteger() Method

The Number.isSafeInteger() method checks whether the passed value is a safe integer.

A safe integer means an integer that can be represented exactly as a 64-bit floating-point number defined by the IEEE 754 international standard.
All integers from -(2^53 - 1) through (2^53 - 1) are included in safe integers.

Number.isSafeInteger(10);                  // true
Number.isSafeInteger(Math.pow(2, 53) - 1); // true
Number.isSafeInteger(Math.pow(2, 53));     // false
Number.isSafeInteger(Infinity);            // false
Number.isSafeInteger(NaN);                 // false
Number.isSafeInteger(3.14);                // false

Math.pow() is a method of the Math object that performs exponentiation.
For example, Math.pow(2, 53) returns 2^53.

This method is not supported in Safari or Internet Explorer.

JavaScript Number Methods

Method Description
Number.parseFloat() Parses a string and returns the numeric part contained in the string as a real number.
Number.parseInt() Parses a string and returns the numeric part contained in the string as an integer.
Number.isNaN() Checks whether the passed value is NaN.
Number.isFinite() Checks whether the passed value is a finite number.
Number.isInteger() Checks whether the passed value is an integer.
Number.isSafeInteger() Checks whether the passed value is a safe integer.

Number.prototype Methods

Every Number instance inherits methods and properties from Number.prototype.
The most commonly used representative Number.prototype methods are as follows.

Method Description
toExponential() Converts a Number instance to exponential notation and returns the value as a string.
toFixed() Fixes the number of decimal places of a Number instance to the passed value and returns the value as a string.
toPrecision() Fixes the total number of digits in the integer and decimal parts of a Number instance to the passed value and returns the value as a string.
toString() Returns the value of a Number instance as a string.
valueOf() Returns the value held by a Number instance.

toExponential() Method

This method converts the value of a Number instance to exponential notation and returns the value as a string.
The passed value is used as the number of decimal places in exponential notation.

Prototype

numObj.toExponential([numberOfDecimalPlaces])
var num = 12.3456;       // creates a Number instance.
num.toExponential();     // 1.23456e+1
num.toExponential(2);    // 1.23e+1
num.toExponential(4);    // 1.2346e+1
12.3456.toExponential(); // 1.23456e+1

toFixed() Method

This method fixes the number of decimal places of a Number instance to the passed value and returns the value as a string.

Prototype

numObj.toFixed([numberOfDecimalPlaces])
var num = 3.14159265;  // creates a Number instance.
num.toFixed();         // 3
num.toFixed(2);        // 3.14
num.toFixed(4);        // 3.1416
3.14159265.toFixed(6); // 3.141593

toPrecision() Method

This method fixes the total number of digits in the integer and decimal parts of a Number instance to the passed value and returns the value as a string.

Prototype

numObj.toPrecision([totalDigits])
var num = 3.14159265;      // creates a Number instance.
num.toPrecision();         // 3.14159265
num.toPrecision(2);        // 3.1
num.toPrecision(4);        // 3.142
3.14159265.toPrecision(6); // 3.14159

toString() Method

This method returns the value of a Number instance as a string.
It first converts the value to the radix corresponding to the passed value and then returns that value as a string.

Prototype

numObj.toString([radix])
var num = 255;       // creates a Number instance.
num.toString();      // 255
(255).toString();    // 255
(3.14).toString();   // 3.14
num.toString(2);     // 11111111
(100).toString(16);  // 64
(-0xff).toString(2); // -11111111

When using the toString() method on a number literal, you must wrap the number literal in parentheses (()).
Otherwise, JavaScript will throw a SyntaxError and stop the program.

valueOf() Method

This method returns the value held by a Number instance.

Prototype

numObj.valueOf()
var numObj = new Number(123); // creates a Number instance with the value 123.
typeof numObj;                // object
var num = numObj.valueOf();
num;                          // 123
typeof num;                   // number