JavaScript Number Methods

There are many number methods available to work with number datatype.

The toString() Method

This method is used to convert number value to string. All methods are available for all types of numbers like float, double etc

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
    var temperature = 45.5;
    var convert = temperature.toString();

	document.getElementById('demo').innerHTML=typeof convert;
</script>
</body>
</html>

Download the code Run the code

The toExponential() Method

This method is used to convert number value to exponential format as a string.
It takes one optional parameter for number of characters after decimal places

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
    var temperature = 45.5;

	document.getElementById('demo').innerHTML=temperature.toExponential() + "<br />" +
temperature.toExponential(1)  + "<br />" +
temperature.toExponential(4);
</script>
</body>
</html>

Download the code Run the code

The toFixed() Method

This method is used to convert float number value with fixed number of characters after decimal points.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
    var temperature = 45.56;

	document.getElementById('demo').innerHTML=temperature.toFixed(0) + "<br />" +
temperature.toFixed(1)  + "<br />" +
temperature.toFixed(2)  + "<br />" +
temperature.toFixed(3);
</script>
</body>
</html>

Download the code Run the code

The toPrecision() Method

This method is returns number with given number of characters as parameter.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
    var temperature = 45.56;

	document.getElementById('demo').innerHTML=temperature.toPrecision(1) + "<br />" +
temperature.toPrecision(2)  + "<br />" +
temperature.toPrecision(3)  + "<br />" +
temperature.toPrecision(4)  + "<br />" +
temperature.toPrecision(5);
</script>
</body>
</html>

Download the code Run the code

toPrecision() cannot take 0 as parameter value, it throws an error “RangeError: precision 0 out of range”

The valueOf() Method

This method is represent number as number value.

Generally there is no need to call this method in coding.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
    var temperature = 45.56;

	document.getElementById('demo').innerHTML= temperature.valueOf();
</script>
</body>
</html>

Download the code Run the code

JavaScript Number Type Casting Methods

There are 3 methods that are used to convert variable to numbers.

They are

  • Number
  • parseInt
  • parseFloat

The Number() Method

This method is used to convert a variable to number type

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
    var temperature = "45.5";
    var bool = true;
    var str = "Hello World";
    	
	document.getElementById('demo').innerHTML= Number(temperature) + '<br>' +
    Number(bool) + '<br>' +
    Number(str);
</script>
</body>
</html>

Download the code Run the code

The parseInt() Method

This method is used to convert a variable to integer i.e. whole number without decimal point

<!DOCTYPE html>
<html>
<body>

The parseInt method converts variable to whole number
<p id="demo"></p>

<script>
    var temperature = "45.5";
    var fahrenheit  = "135 5";
    var bool = true;
    var str = "Hello World";
    
    	
	document.getElementById('demo').innerHTML= parseInt(temperature) + '<br>' +
    parseInt(fahrenheit) + '<br>' +
    parseInt(bool) + '<br>' +
    parseInt(str);
</script>
</body>
</html>

Download the code Run the code

The parseFloat() Method

This method is used to convert a string variable to number with or without decimal point.

<!DOCTYPE html>
<html>
<body>

The parseFloat method converts string variables to numbers
<p id="demo"></p>

<script>
    var temperature = "45.5";
    var fahrenheit  = "135 5";
    var bool = true;
    var str = "Hello World";
    
    	
	document.getElementById('demo').innerHTML= parseFloat(temperature) + '<br>' +
    parseFloat(fahrenheit) + '<br>' +
    parseFloat(bool) + '<br>' +
    parseFloat(str);
</script>
</body>
</html>

Download the code Run the code

Number Properties

Below are list of properties available for Numbers

Property			Description
MAX_VALUE			Returns the largest number possible in JavaScript
MIN_VALUE			Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY	Represents negative infinity (returned on overflow)
NaN					Represents a "Not-a-Number" value
POSITIVE_INFINITY	Represents infinity (returned on overflow)

All above properties are available with Number only. It will not work with number variables

<!DOCTYPE html>
<html>
<body>

The parseFloat method converts string variables to numbers
<p id="demo"></p>

<script>
	document.getElementById('demo').innerHTML = Number.MAX_VALUE + '<br>' +
Number.MIN_VALUE + '<br>' +
Number.NEGATIVE_INFINITY + '<br>' +
Number.NaN + '<br>' +
Number.POSITIVE_INFINITY;
</script>
</body>
</html>

Download the code Run the code

<<< JavaScript NumbersJavaScript Math Object >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .