JavaScript Number Methods


Global Methods

JavaScript global functions can be used on all JavaScript data types.

These are the most relevant methods, when working with numbers.

MethodDescription
Number()Returns a number, converted from its argument.
parseFloat()Parses its argument and returns a floating point number
parseInt()Parses its argument and returns an integer

The Number() Method

The Number(), can be used to convert JavaScript variables to numbers

Example

<script type="text/javascript">
    x = true;
    document.write(Number(x) +"<br⁄>");        // returns 1
    x = false;     
    document.write(Number(x) +"<br⁄>");        // returns 0
    x = new Date();
    document.write(Number(x) +"<br⁄>");        // returns 1404568027739
    x = "10"
    document.write(Number(x) +"<br⁄>");        // returns 10
    x = "10 20"
    document.write(Number(x));        // returns NaN 
</script>
Output


The parseInt() Method

The parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned.

Example

<script type="text/javascript">
  document.write(parseInt("12") + "<br>");        // returns 12
  document.write(parseInt("12.33") + "<br>");     // returns 12
  document.write(parseInt("12 6") + "<br>" );     // returns 12
  document.write(parseInt("12 years") + "<br>");  // returns 12
  document.write(parseInt("month 12"));           // returns NaN
</script>
Output


The parseFloat() Method

The parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned.

Example

<script type="text/javascript">
  document.write(parseFloat("12") + "<br>");        // returns 12
  document.write(parseFloat("12.33") + "<br>");     // returns 12.33
  document.write(parseFloat("12 6") + "<br>" );     // returns 12
  document.write(parseFloat("12 years") + "<br>");  // returns 12
  document.write(parseFloat("month 12"));           // returns NaN
</script>
Output


Number Methods

The Number object contains only the default methods that are part of every object's definition.

MethodDescription
constructor()Returns the function that created this object's instance. By default this is the Number object.
toExponential()Forces a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation.
toFixed()Formats a number with a specific number of digits to the right of the decimal.
toLocaleString()Returns a string value version of the current number in a format that may vary according to a browser's locale settings.
toPrecision()Defines how many total digits (including digits to the left and right of the decimal) to display of a number.
toString()Returns the string representation of the number's value.
valueOf()Returns the number's value.

The constructor() Method

This method returns a reference to the Number function that created the instance's prototype.

Syntax

number.constructor()

Example

<script type="text/javascript">
   var num = new Number( 234.134 );
   document.write("num.constructor() is : " + num.constructor); 
</script>
Output


The toExponential() Method

This method returns a string representing the number object in exponential notation

Syntax

number.toExponential( [fractionDigits] )

Example

<script type="text/javascript">
   var num = 22.34;
   var val = num.toExponential(); 
   document.write("num.toExponential() is : " + val + "<br />"); 
 
   val = num.toExponential(4); 
   document.write("num.toExponential(4) is : " + val + "<br />" ); 

   val = num.toExponential(2); 
   document.write("num.toExponential(2) is : " + val + "<br />"); 

   val = 22.34.toExponential(); 
   document.write("77.1234.toExponential()is : " + val + "<br />");

   val = 22.34.toExponential(); 
   document.write("22 .toExponential() is : " + val + "<br />"); 
</script>
Output

The toFixed() Method

This method formats a number with a specific number of digits to the right of the decimal.

Syntax

number.toFixed( [digits] )

Digits: The number of digits to appear after the decimal point.

Example

<script type="text/javascript">
 var num=22.3489;
 document.write("num.toFixed() is : " + num.toFixed() + "<br />"); 
 document.write("num.toFixed(6) is : " + num.toFixed(8) + "<br />"); 
 document.write("num.toFixed(1) is : " + num.toFixed(2) + "<br />"); 

 document.write("(2.34e+20).toFixed(2) is : " +(2.34e+20).toFixed(3) + "<br />"); 
 document.write("(2.34e-11).toFixed(2) is : " + (2.34e-11).toFixed(3) + "<br />"); 
</script>
Output

The toLocaleString() Method

This method converts a number object, into a human readable string representing the number using the locale of the environment.

Syntax

number.toLocaleString()

Example

<script type="text/javascript">
 var num=new Number(22.3489);
 document.write( num.toLocaleString());
</script>
Output

The toPrecision() Method

The toPrecision() returns a string, with a number written with a specified length.

Syntax

number.toPrecision( [ precision ] )

Example

<script type="text/javascript">
 var num = new Number(2.123456);
 document.write("num.toPrecision() is: " + num.toPrecision() +"<br />");
 document.write("num.toPrecision(6) is: " + num.toPrecision(6) +"<br />"); 
 document.write("num.toPrecision(4) is: " + num.toPrecision(4) +"<br />"); 
 document.write("num.toPrecision(1) is: " + num.toPrecision(1) +"<br />"); 
</script>
Output

The toString() Method

This method returns a string representing the specified object. The toString method parses its first argument, and attempts to return a string representation in the specified radix (base).

Syntax

number.toString( [radix] )

Example

<script type="text/javascript">
   var num = new Number(25);
   document.write("num.toString() is: " + num.toString() +"<br />");
   document.write("num.toString(2) is: " + num.toString(2) +"<br />"); 
   document.write("num.toString(4) is: " + num.toString(4) +"<br />"); 
 </script>
Output

The valueOf() Method

This method returns the primitive value of the specified number object.

Syntax

number.valueOf()

Example

<script type="text/javascript">
   var num = new Number(25.123456);
   document.write("num.valueOf() is: " + num.valueOf());
 </script>
Output

Share this article on