Number.parseInt() method parses a string argument and returns an integer of the specified radix or base.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

Number.parseInt(string,[ radix])
					

参数

string
The value to parse. If this argument is not a string, then it is converted to one using the ToString abstract operation. Leading whitespace in this argument is ignored.
radix 可选
An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string . Be careful—this does not default to 10 !

返回值

An integer parsed from the given string .

radix < 2 or bigger than 36 , and the first non-whitespace character cannot be converted to a number, NaN 被返回。

Polyfill

if (Number.parseInt === undefined) {
    Number.parseInt = window.parseInt
}
					

范例

Number.parseInt vs parseInt

This method has the same functionality as the global parseInt() 函数:

Number.parseInt === parseInt // true
					

and is part of ECMAScript 2015 (its purpose is modularization of globals). Please see parseInt() for more detail and examples.

规范

规范
ECMAScript (ECMA-262)
The definition of 'Number.parseInt' in that specification.

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
parseInt Chrome 34 Edge 12 Firefox 25 IE No Opera 21 Safari 9 WebView Android ≤37 Chrome Android 34 Firefox Android 25 Opera Android 21 Safari iOS 9 Samsung Internet Android 2.0 nodejs 0.12

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: