SoFunction
Updated on 2025-03-10

A comprehensive summary of various digital conversions in JavaScript

Use the basic mode of toString to convert:

ECMAScript describes the toString of numeric types as follows:

The toString() method of Number type is quite special. It has two modes, namely the default mode and the base mode. In the default mode, the toString() method simply outputs numeric values ​​(whether it is an integer, a floating point, or a scientific notation method) using the corresponding string, as shown below:

var iNum1 = 10;
var iNum2 = 10.0;
alert(()); //Output "10"alert(()); //Output "10"

Note: In the default mode, the toString() method of Number type returns a decimal representation of the number regardless of the initial notation used to declare the number. Therefore, the output of the numerical declared in octal or hexadecimal literals is in decimal form.
Using the base mode of the toString() method of the Number type, you can use different bases to output numbers, such as the binary base is 2, the octal base is 8, and the hexadecimal base is 16.
The base is just another addition to the cardinality to be converted into. It is a parameter toString() method:

var iNum = 10;
alert((2)); //Output "1010"alert((8)); //Output "12"alert((16)); //Output "A"

In the previous example, the number 10 is output in 3 different forms, namely binary, octal and hexadecimal. HTML represents each color in hexadecimal, and this feature is very useful when processing numbers in HTML.

Note: Calling toString(10) on a number is the same as calling toString(), and they return the decimal form of the number.

The above is the entire content of this article. I hope the content of this article will be of some help to everyone’s study or work. If you have any questions, you can leave a message to communicate. Thank you for your support!