JavaScript Reference ยท Number and BigInt
Number.parseFloat()
Reference for Number.parseFloat(): parsing decimal numbers from the start of a string, partial parsing, exponent notation, NaN results and how it differs from Number().
Number.parseFloat() reads a string from the left and returns the decimal number it finds at the start. It skips leading whitespace, accepts an optional sign, digits, a decimal point and an exponent, and stops at the first character that cannot continue the number. If there is no number at the start, the result is NaN.
It is the very same function object as the global parseFloat(); the copy on Number exists so that number-related functions live in one place.
Number.parseFloat(string)
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Number.parseFloat(string)
| Name | Description |
|---|---|
string |
The value to parse. Non-strings are converted to a string first (null becomes "null", giving NaN). |
Return value: the parsed number, or NaN if the first non-whitespace characters cannot start a decimal number.
Exceptions: none for ordinary values. A Symbol or an object whose string conversion throws will throw.
Usage notes
- Partial parsing is allowed.
"12.5px"gives12.5. If you want to reject trailing junk, useNumber(text), which returnsNaNfor it. - Empty strings:
parseFloat("")isNaN, butNumber("")is0. Check emptiness yourself when it matters. - Decimal only. Hex (
"0x10") gives0, because parsing stops atx. Binary and octal prefixes are also not recognised. UseparseInt()with a radix for integers in other bases. - No numeric separators.
"1_000"gives1and"1,234"gives1. Strip separators first, or useIntl.NumberFormatknowledge to parse locale input. - Accepted forms: optional
+or-, digits with at most one., optional exponent such ase5orE-3, orInfinity/-Infinity. It does not acceptNaNas text. - No radix parameter, unlike
parseInt(). - Binary floating point: the result is the nearest double, so
parseFloat("0.1") + parseFloat("0.2")is still0.30000000000000004. - Compare with
Number.isNaN()to detect failure;NaNis not equal to itself.
Browser support
Supported in all current browsers. The Number.parseFloat alias arrived with ES2015, while the global function has existed since the beginning.