Number.parseFloat() method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN .

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.parseFloat(string)
					

参数

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.

返回值

A floating point number parsed from the given string .

Or NaN when the first non-whitespace character cannot be converted to a number.

Polyfill

if (Number.parseFloat === undefined) {
  Number.parseFloat = parseFloat;
}
					

范例

Number.parseFloat vs parseFloat

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

Number.parseFloat === parseFloat; // true
					

This method is also part of ECMAScript 2015. (Its purpose is modularization of globals.)

parseFloat() for more detail and examples.

规范

规范
ECMAScript (ECMA-262)
The definition of 'Number.parseFloat' 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
parseFloat 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

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: