Javascript Number


We can use number methods on any format of numbers.

toString method in javascript

toString method is used to convert enire number to the string format.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="output">
</p>
<script>
var x=156;
document.getElementById("output")
.innerHTML=
x.toString() + "<br>" + ;
(100+56)toString() + "<br>" + ;
(156)toString();
</script>
</body>
</html>

Example:

toExponential() method in javascript

toExponential() method is used to convert number to the exponential form.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="out">
</p>
<script>
var x=15.7896;
document.getElementById("out")
.innerHTML=
x.toExponential(2) + "<br>" +
x.toExponential(4) + "<br>" +
x.toExponential(7); </script>
</body>
</html>

Example:

toFixed() method in javascript

toFixed() method is used get number in output with fixed number of decimal outputs.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="out">
</p>
<script>
var x=15.7896;
document.getElementById("out")
.innerHTML=
x.toFixed(2) + "<br>" +
x.toFixed(4) + "<br>" +
x.toFixed(7); </script>
</body>
</html>

Example:

toPrecision() method in javascript

toPrecision() method returns string in number of digits as mentioned.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="outp">
</p>
<script>
var x=15.7896;
document.getElementById("outp")
.innerHTML=
x.toPrecision(2) + "<br>" +
x.toPrecision(4) + "<br>" +
x.toPrecision(7); </script>
</body>
</html>

Example:

parseInt() method in javascript

parseInt() method gives a whole number as the output.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="outp">
</p>
<script>
document.getElementById("outp")
.innerHTML=
parseInt(10) + "<br>" +
parseInt(10.6789);
</script>
</body>
</html>

Example:

parseFloat() method in javascript

parseFloat() method is used to return only fist mentioned number.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="outp">
</p>
<script>
document.getElementById("outp")
.innerHTML=
parseFloat(10) + "<br>" +
parseFloat(10,70,60);
</script>
</body>
</html>

Example:

valueOf() method in javascript

valueOf() method is used number as number in output.
<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="output">
</p>
<script>
var x=156;
document.getElementById("output")
.innerHTML=
x. valueOf() + "<br>" + ;
(100+56) valueOf() + "<br>" + ;
(156) valueOf();
</script>
</body>
</html>

Example: