Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Global Functions and Values

The parseFloat() function

Reference for JavaScript parseFloat(): converting strings to decimal numbers, partial parsing, exponents, Infinity, NaN results and comparison with Number().

parseFloat() reads a decimal number from the start of a string and returns it as a number. Like parseInt(), it ignores leading whitespace and stops at the first character that does not belong to a number, so "3.5em" becomes 3.5.

It always works in base 10. If the text does not start with a number, the result is NaN.

JavaScript
parseFloat("3.14abc");   // 3.14
parseFloat("1e3");       // 1000

Try it

JS Parsing decimals

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

No output.

JS parseFloat or Number?

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

No output.

Syntax

JavaScript
parseFloat(string)
Name Description
string The value to parse. Non-strings are converted to strings first. Leading whitespace is skipped.

Return value: a number, or NaN if no number can be read at the start of the string.

Exceptions: none for ordinary values.

Usage notes

  • Accepted syntax: an optional sign, digits with at most one decimal point, and an optional exponent (e or E followed by an optional sign and digits). The literal Infinity (with a sign) is also recognized.
  • Only the longest valid prefix is used. "1.2.3" gives 1.2, and "1,5" gives 1, because a comma is not a decimal separator in JavaScript.
  • No hex, binary or octal. parseFloat("0x10") is 0; use parseInt() with a radix or Number() for those.
  • Empty and blank strings give NaN, whereas Number("") gives 0. That difference can matter when validating form fields.
  • Binary floating point still applies. parseFloat("0.1") + parseFloat("0.2") is 0.30000000000000004. For money, store integer cents or use a decimal library.
  • Use Number() for strict parsing. It rejects trailing text, so Number("12.5kg") is NaN, which is usually what you want for validation.
  • Locale-formatted text such as "1.234,56" is not understood. Strip separators yourself or use Intl.NumberFormat for output.
  • It is the same function as Number.parseFloat.

Browser support

parseFloat() works in every browser and JavaScript runtime.