Home Web Front-end JS Tutorial Comprehensive summary of various number system conversions in JavaScript (graphic tutorial)

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

May 19, 2018 am 09:31 AM
javascript js Graphics and text

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:

js Implementing 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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

See all articles