Loading Please Wait...
JavaScript provides the following number methods that can be used on all JavaScript numbers:
The toString() method returns a number as a string. All number methods can be used on any type of numbers (literals, variables, or expressions):
let x = 123;
x.toString();
(123).toString();
(100 + 23).toString();
By default, JavaScript displays numbers as base 10 decimals. But you can use the toString() method to output numbers from base 2 to base 36. Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Binary is base 2.
let myNumber = 32;
myNumber.toString(32);
myNumber.toString(16); // Hexadecimal
myNumber.toString(12);
myNumber.toString(10); // Decimal
myNumber.toString(8); // Octaal
myNumber.toString(2); // Binary
toExponential() returns a string, with a number rounded and written using exponential notation. A parameter defines the number of characters behind the decimal point:
let a = 9.656;
a.toExponential(2);
a.toExponential(4);
a.toExponential(6);
toFixed() returns a string, with the number written with a specified number of decimals. toFixed(2) is perfect for working with money.
let a = 9.656;
a.toFixed(0);
a.toFixed(2);
a.toFixed(4);
a.toFixed(6);
toPrecision() returns a string, with a number written with a specified length.
let a = 9.656;
a.toPrecision();
a.toPrecision(2);
a.toPrecision(4);
a.toPrecision(6);
valueOf() returns a number as a number.
let a = 123;
a.valueOf();
(123).valueOf();
(100 + 23).valueOf();
The Number() method can be used to convert JavaScript variables to numbers.
Number(true);
Number(false);
Number("10");
Number(" 10");
Number("10 ");
Number(" 10 ");
Number("10.33");
Number("10,33");
Number("10 33");
Number("John");
Number(new Date("1970-01-01"));
parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned.
parseInt("-10");
parseInt("-10.33");
parseInt("10");
parseInt("10.33");
parseInt("10 20 30");
parseInt("10 years");
parseInt("years 10");
parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned.
parseFloat("10");
parseFloat("10.33");
parseFloat("10 20 30");
parseFloat("10 years");
parseFloat("years 10");
The Number.isInteger() method returns true if the argument is an integer.
Number.isInteger(10);
Number.isInteger(10.5);
A safe integer is an integer that can be exactly represented as a double precision number. The Number.isSafeInteger() method returns true if the argument is a safe integer.
Number.isSafeInteger(10);
Number.isSafeInteger(12345678901234567890);
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved