File size: 1,867 Bytes
780c9fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
---
title: Number.parseFloat()
short-title: parseFloat()
slug: Web/JavaScript/Reference/Global_Objects/Number/parseFloat
page-type: javascript-static-method
browser-compat: javascript.builtins.Number.parseFloat
sidebar: jsref
---
The **`Number.parseFloat()`** static method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns {{jsxref("NaN")}}.
{{InteractiveExample("JavaScript Demo: Number.parseFloat()")}}
```js interactive-example
function circumference(r) {
if (Number.isNaN(Number.parseFloat(r))) {
return 0;
}
return parseFloat(r) * 2.0 * Math.PI;
}
console.log(circumference("4.567abcdefgh"));
// Expected output: 28.695307297889173
console.log(circumference("abcdefgh"));
// Expected output: 0
```
## Syntax
```js-nolint
Number.parseFloat(string)
```
### Parameters
- `string`
- : The value to parse, [coerced to a string](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_coercion). Leading {{Glossary("whitespace")}} in this argument is ignored.
### Return value
A floating point number parsed from the given `string`.
Or {{jsxref("NaN")}} when the first non-whitespace character cannot be converted to a number.
## Examples
### Number.parseFloat vs. parseFloat
This method has the same functionality as the global {{jsxref("parseFloat()")}} function:
```js
Number.parseFloat === parseFloat; // true
```
Its purpose is modularization of globals.
See {{jsxref("parseFloat()")}} for more detail and examples.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Polyfill of `Number.parseFloat` in `core-js`](https://github.com/zloirock/core-js#ecmascript-number)
- [es-shims polyfill of `Number.parseFloat`](https://www.npmjs.com/package/number.parsefloat)
- {{jsxref("Number")}}
- {{jsxref("parseFloat()")}}
|