Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
Number.parseFloat(string)

Try it

JS Reading numbers from text

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS parseFloat() versus Number()

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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" gives 12.5. If you want to reject trailing junk, use Number(text), which returns NaN for it.
  • Empty strings: parseFloat("") is NaN, but Number("") is 0. Check emptiness yourself when it matters.
  • Decimal only. Hex ("0x10") gives 0, because parsing stops at x. Binary and octal prefixes are also not recognised. Use parseInt() with a radix for integers in other bases.
  • No numeric separators. "1_000" gives 1 and "1,234" gives 1. Strip separators first, or use Intl.NumberFormat knowledge to parse locale input.
  • Accepted forms: optional + or -, digits with at most one ., optional exponent such as e5 or E-3, or Infinity/-Infinity. It does not accept NaN as text.
  • No radix parameter, unlike parseInt().
  • Binary floating point: the result is the nearest double, so parseFloat("0.1") + parseFloat("0.2") is still 0.30000000000000004.
  • Compare with Number.isNaN() to detect failure; NaN is 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.