Comprehensive summary of various number system conversions in JavaScript (graphic tutorial)

亚连
Release: 2018-05-19 09:31:51
Original
1707 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 the default mode, the toString() method of the Number type returns the decimal representation of the number, no matter what representation the number is originally declared in. 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 argument to 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 , the number 10 is output in 3 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 what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

jsImplementing mutual transfer of Json code between front and backend

$http service Post method passingjson parameter case detailed explanation

What are the frequent mistakes made by novice JS users

The above is the detailed content of Comprehensive summary of various number system conversions in JavaScript (graphic tutorial). 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!