Home  >  Article  >  Web Front-end  >  How to convert a string to an integer using JavaScript's parseInt()

How to convert a string to an integer using JavaScript's parseInt()

不言
不言Original
2018-12-22 13:34:435996browse

parseInt is a global How to convert a string to an integer using JavaScripts parseInt() function that can convert a string into an integer. The global function is a function that can be accessed and used anywhere in the How to convert a string to an integer using JavaScripts parseInt() program. Next, let’s take a look at parseInt ()Convert string to integer specific usage.

How to convert a string to an integer using JavaScripts parseInt()

Usage of parseInt

parseInt can be used in the following ways.

parseInt(str)
parseInt(str, int)

Usage of parseInt(str)

str is the abbreviation of string and represents a string. When

parseInt("123");

is specified, the string 123 will be converted to the data type of the integer 123.

parseInt(str, int) uses

The part of int is the base.

Specifies that the number before conversion is represented by a decimal number.

The numerical value that specifies this base is called the base.

The base can specify binary, octal, decimal, and hexadecimal numbers.

Binary numbers are numerical values ​​represented by 0 and 1.

Octal numbers are represented by numbers from 0 to 7.

Decimal numbers are expressed using the values ​​from 0 to 9 that we use daily.

Hexadecimal numbers are represented by numbers starting from 0x or 0X and starting from 0 to 9 or symbols from A to F (letters from a to f also mean the same thing)

For example, if 1 is expressed as a decimal number, the binary number is 1, the octal number is 01, and the hexadecimal number is 0x1.

You can use these 2, 8, 10, and 16 as parameters of int, that is, as base numbers.

For example, when using parseInt to convert hexadecimal 0x8, it is written as follows.

parseInt("0x8");

The result will be displayed as the decimal number 8.

Let’s look at the specific usage examples of parseInt

Use decimal numbers to represent the value of 8, and use the parseInt function to convert it to decimal numbers

parseInt("1000", 2);将以二进制显示的1000转换为十进制。
parseInt("010", 8);将以八进制显示的010转换为十进制。
parseInt("8", 10);转换8以十进制显示为十进制。
parseInt("0x8", 16);将十六进制0x8转换为十进制数。

The displayed result will be: 8, 8, 8, 8

In fact, there are similar functions such as: parseFloat function and Number function.

parseFloat function

Different from parseInt, parseFloat function Floating point numbers can be used.

Floating point is decimal point notation, if it is an integer value of 1, it is displayed as 1.0.

Number function

Like the parseFloat function, the Number function can also handle floating point numbers.

However, unlike parseInt and parseFloat, it cannot correspond to the numerical value of the string, but it can analyze the object.


The above is the detailed content of How to convert a string to an integer using JavaScript's parseInt(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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