Google News
logo
Javascript Numbers
The Number is a primitive data type in JavaScript. Number type represents integer, floating point, hexadecimal, octal or exponential value. JavaScript number object follows IEEE standard to represent the floating-point numbers.
var n=new Number(some-number);
Example :
<html>
<head>
<title>Javascript Numbers</title>
</head>
<body>
 
<p id="int"></p>
<p id="float"></p>
<p id="hex"></p>
<p id="exp"></p>
<p id="octal"></p>
    
<script type="text/javascript">  
var int = 100;
var float = 100.2;
var hex = 0x24f;
var exponential = 1.26e5;
var octal = 070;
 
document.getElementById("int").innerHTML = int; 
document.getElementById("float").innerHTML = float; 
document.getElementById("hex").innerHTML = hex;
document.getElementById("exp").innerHTML = exponential;
document.getElementById("octal").innerHTML = octal; 
</script>
 
</body>
</html>
Output :

100

100.2

591

126000

56

Number Properties :
Property Description
MAX_VALUE Specifies the largest maximum value.
MIN_VALUE Specifies the largest minimum value.
POSITIVE_INFINITY Specifies positive infinity.
NEGATIVE_INFINITY Specifies negative infinity.
NaN Represents a value that is not a number.
Example :
<html>
<head>
<title>Number Properties</title>
</head>
<body>
    
<script type="text/javascript"> 
 
document.writeln(' Max Value : ' + Number.MAX_VALUE +
'<br /> Min Value : ' + Number.MIN_VALUE +
'<br /><br /> Negative Infinity : ' + Number.NEGATIVE_INFINITY +
'<br /> Positive Infinity : ' + Number.POSITIVE_INFINITY +
'<br /><br /> NaN : ' + Number.NaN
);

</script>
 
</body>
</html>
Output :

Max Value : 1.7976931348623157e+308

Min Value : 5e-324


Negative Infinity : -Infinity

Positive Infinity : Infinity


NaN : NaN

Number Methods :
Property Description
toExponential() Exponential value.
toFixed() A specific number of digits to the right of the decimal.
toPrecision() Number as a string with specified total digits.
toString() Converts number into string.
valueOf() Coverts other type of value into number.
toLocaleString() A string value of the current number
Example :
<html>
<head>
<title>Number Methods</title>
</head>
<body>
    
<script type="text/javascript">
         var num=99.234;
         var val = num.toExponential(); 
         document.write("num.toExponential() is : " + val );
         document.write("<br /><br />"); 
 
var num_2=190.1232;
         document.write("num.toFixed() is : " + num_2.toFixed()); 
         document.write("<br />"); 
 
 document.write("num.toFixed(5) is : " + num_2.toFixed(5)); 
         document.write("<br /><br />"); 
 
 
var num_3 = new Number(9.123456789);
         document.write("num.toPrecision() is " + num_3.toPrecision());
         document.write("<br />"); 
 
         document.write("num.toPrecision(4) is " + num_3.toPrecision(4));
         document.write("<br /><br />"); 
 
 
var num_4 = new Number(9);
         document.write("num.toString() is " + num_4.toString());
         document.write("<br />"); 
         
         document.write("num.toString(4) is " + num_4.toString(4)); 
         document.write("<br /><br />"); 
 
var num_5 = new Number(27.227430);
         document.write("num.valueOf() is " + num_5.valueOf());
document.write("<br /><br />"); 
 
var num_6 = new Number(153.1234);
         document.write("num.toLocaleString() is " + num_6.toLocaleString()); 
 
</script>
 
</body>
</html>
Output :

num.toExponential() is : 9.9234e+1


num.toFixed() is : 190

num.toFixed(5) is : 190.12320


num.toPrecision() is 9.123456789

num.toPrecision(4) is 9.123

num.toString() is 9

num.toString(4) is 21


num.valueOf() is 27.22743


num.toLocaleString() is 153.123