Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the parseInt () function in JavaScript

Shulou Source: shulou.com Published: 2022-05-31 10:40:10 09月15日 Update

This article mainly introduces the relevant knowledge of "how to use the parseInt () function in JavaScript". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the parseInt () function in JavaScript" can help you solve the problem.

JavaScript parseInt () function

JavaScript Global object reference Manual

Definition and usage

The parseInt () function parses a string and returns an integer.

Syntax parseInt (string, radix) parameter describes that string is required. The string to be parsed. Radix

Optional. Represents the cardinality of the number to parse. The value is between 2 and 36.

If the parameter is omitted or its value is 0, the number is parsed on the basis of 10. If it starts with "0x" or "0X", it will have a base of 16.

If the parameter is less than 2 or greater than 36, parseInt () returns NaN.

Return value

Returns the parsed number.

Description

When the value of the parameter radix is 0, or when it is not set, parseInt () determines the cardinality of the number based on string.

For example, if string starts with "0x", parseInt () parses the rest of the string into hexadecimal integers. If string starts with 0, ECMAScript v3 allows an implementation of parseInt () to parse subsequent characters into octal or hexadecimal numbers. If string starts with a number from 1 to 9, parseInt () parses it to a decimal integer.

Tips and comments

Note: only the first number in the string will be returned.

Note: spaces at the beginning and end are allowed.

Tip: if the first character of the string cannot be converted to a number, parseFloat () returns NaN.

Example

In this example, we will use parseInt () to parse different strings:

ParseInt ("10"); / return 10parseInt ("19", 10); / / return 19 (10) parseInt ("11", 2); / / return 3 (2) parseInt ("17", 8); / / return 15 (8) parseInt ("1f", 16); / / return 31 (16) parseInt ("010") / / undecided: return 10 or 8 about "how to use the parseInt () function in JavaScript". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Number character function parameter string beginning cardinality integer comment knowledge different hexadecimal industry prompt practical between global octal content decimal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker MySQL Shulou Information MariaDB