Home > Web Front-end > JS Tutorial > How to convert string to number in javascript

How to convert string to number in javascript

青灯夜游
Release: 2023-01-04 09:34:47
Original
7691 people have browsed it

Method: 1. Use the Number() function, the syntax format is "Number (string object)"; 2. Use the parseInt() function, the syntax format is "parseInt (string object)"; 3. Use parseFloat () function, syntax format "parseFloat (string object)".

How to convert string to number in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Convert javascript string to number

①Use Number() function

Pass Number The () conversion function passes in a string, and it will try to convert it into an integer or floating point number literal. This method can only convert based on decimal, and non-numeric characters cannot appear in the string, otherwise NaN will be returned.

Number("023") // returns 23
Number(023) // returns 19
Copy after login

Note: 023 is actually an octal number, and no matter what you do, it returns 19; the same is true for hexadecimal numbers without single or double quotes.

② Use the parseInt() function

It is a global function, does not belong to any class method, and only parses integers. If the string prefix is ​​"0x" or "0X", parseInt() interprets it as a hexadecimal number. It skips any number of leading spaces, parses as many numeric characters as possible, ignores what follows, and returns NaN if the first non-space character is a non-numeric character. For example:

How to convert string to number in javascript

[Recommended learning: javascript advanced tutorial]

parseInt() can also receive a second optional parameter, This parameter specifies the base for number conversion. The legal value range is 2~36, for example:

How to convert string to number in javascript

③ Use the parseFloat() function:

It is also a global function and does not belong to any class method. It can parse integers and floating point numbers. It does not recognize the hexadecimal prefix "0x" or "0X". It also skips any number of leading spaces when parsing, parses as many numeric characters as possible, ignores what follows, and returns NaN if the first non-space character is a non-numeric character. For example:

How to convert string to number in javascript

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to convert string to number 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