Number datatype Methods
toString() returns a number as a string.
var x = 123;
x.toString(); // returns 123 from variable x
(123).toString(); // returns 123 from literal (100 + 23).toString(); // returns 123 from expression 100 + 23
x.toString(); // returns 123 from variable x
(123).toString(); // returns 123 from literal (100 + 23).toString(); // returns 123 from expression 100 + 23
toExponential() returns a string, with a number rounded and written using exponential notation.
var x = 9.656;
x.toExponential(2); // returns 9.66e+0
x.toExponential(4); // returns9.6560e+0
x.toExponential(6); //returns 9.656000e+0
var y = 123e5; // 12300000
var z = 123e-5; // 0.00123
x.toExponential(2); // returns 9.66e+0
x.toExponential(4); // returns9.6560e+0
x.toExponential(6); //returns 9.656000e+0
var y = 123e5; // 12300000
var z = 123e-5; // 0.00123
toFixed() returns a string, with the number written with a specified number of decimals:
var x = 9.656;
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000
toPrecision() returns a string, with a number written with a specified length:
var x = 9.656;
x.toPrecision(); // returns 9.656
x.toPrecision(2); // returns 9.7
x.toPrecision(4); // returns 9.656
x.toPrecision(6); // returns 9.65600
x.toPrecision(); // returns 9.656
x.toPrecision(2); // returns 9.7
x.toPrecision(4); // returns 9.656
x.toPrecision(6); // returns 9.65600
valueOf() returns a number as a number.
var x = 123;
x.valueOf(); // returns 123 from variable x
(123).valueOf(); // returns 123 from literal 123
(100 + 23).valueOf(); // returns 123 from expression 100 + 23
x.valueOf(); // returns 123 from variable x
(123).valueOf(); // returns 123 from literal 123
(100 + 23).valueOf(); // returns 123 from expression 100 + 23
_________________________
Converting Variables to Numbers
There are 3 JavaScript methods that can be used to convert variables to numbers:
- The Number() method. Returns a number, converted from its argument.
x = true;Number(x); // returns 1
x = false;Number(x); // returns 0
x = new Date();Number(x); // returns 1404568027739
x = "10";Number(x); // returns 10
x = "10 20";Number(x); // returns NaN
x = false;Number(x); // returns 0
x = new Date();Number(x); // returns 1404568027739
x = "10";Number(x); // returns 10
x = "10 20";Number(x); // returns NaN
- The parseInt() method. Parses its argument and returns an integer. returns a whole number. Spaces are allowed. Only the first number is returned:
parseInt("10"); // returns 10
parseInt("10.33"); // returns 10
parseInt("10 20 30"); // returns 10
parseInt("10 years"); // returns 10
parseInt("years 10"); // returns NaN
parseInt("10.33"); // returns 10
parseInt("10 20 30"); // returns 10
parseInt("10 years"); // returns 10
parseInt("years 10"); // returns NaN
- The parseFloat() method. Parses its argument and returns a floating point number. Spaces are allowed. Only the first number is returned:
parseFloat("10"); // returns 10
parseFloat("10.33"); // returns 10.33
parseFloat("10 20 30"); // returns 10
parseFloat("10 years"); // returns 10
parseFloat("years 10"); // returns NaN
parseFloat("10.33"); // returns 10.33
parseFloat("10 20 30"); // returns 10
parseFloat("10 years"); // returns 10
parseFloat("years 10"); // returns NaN
_________________________
Number Properties
<body>
<p id="demo"></p>
<p id="demo2"></p>
<script>
var x = 6;
document.getElementById("demo").innerHTML = x.MAX_VALUE;//undefined
document.getElementById("demo2").innerHTML = Number.MAX_VALUE;//1.7976931348623157e+308
</script>
</body>
</html>
O/P:
undefined
1.7976931348623157e+308
_________________________
No comments:
Post a Comment