Home > Web Front-end > JS Tutorial > Detailed explanation of examples of various number system conversions in JavaScript

Detailed explanation of examples of various number system conversions in JavaScript

黄舟
Release: 2017-08-22 11:54:44
Original
1835 people have browsed it

This article mainly introduces the conversion of various numerical systems in JavaScript, using the base mode of toString for conversion. The differences and similarities between calling toString(10) and calling toString() on numbers are all in this article. As mentioned in, you can check the detailed explanation below for the specific operation steps. Interested friends can refer to it.

Use the base mode of toString for conversion:

ECMAScript describes the toString of the numeric type as follows:

The toString() method of the Number type is special, and it has two types Modes, namely the default mode and the base mode. In default mode, the toString() method simply outputs a numeric value (whether integer, floating point, or scientific notation) with the corresponding string, as follows:


var iNum1 = 10;
var iNum2 = 10.0;
alert(iNum1.toString()); //输出 "10"alert(iNum2.toString()); //输出 "10"
Copy after login

Note: In default mode, the toString() method of the Number type returns the decimal representation of the number, regardless of the representation in which the number was originally declared. Therefore, numbers declared in octal or hexadecimal literal form are output in decimal form.
Using the base mode of the toString() method of the Number type, numbers can be output in different bases, for example, the base of binary is 2, the base of octal is 8, and the base of hexadecimal is 16.
The base is just another addition to the base to be converted to, which is the parameter of the toString() method:


var iNum = 10;
alert(iNum.toString(2)); //输出 "1010"alert(iNum.toString(8)); //输出 "12"alert(iNum.toString(16)); //输出 "A"
Copy after login

In the previous example, with 3 The number 10 is output in three different forms, namely binary form, octal form and hexadecimal form. HTML represents each color in hexadecimal, which is useful when working with numbers in HTML.

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

The above is the detailed content of Detailed explanation of examples of various number system conversions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template