The parseFloat()
function parses an argument (converting it to a string first if needed) and returns a floating point number.
The parseFloat()
function parses an argument (converting it to a string first if needed) and returns a floating point number.
parseFloat(string)
string
The value to parse, coerced to a string. 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.
Note: JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level. parseInt()
and parseFloat()
only differ in their parsing behavior, but not necessarily their return values. For example, parseInt("42")
and parseFloat("42")
would return the same value: a Number
42.
parseFloat
is a function property of the global object.
parseFloat
encounters a character other than a plus sign (+
), minus sign (-
U+002D HYPHEN-MINUS), numeral (0
–9
), decimal point (.
), or exponent (e
or E
), it returns the value up to that character, ignoring the invalid character and characters following it. parseFloat
returns NaN
. parseFloat
can also parse and return Infinity
if the string starts with "Infinity" preceded by none or more white spaces.-1.7976931348623158e+308 - 1.7976931348623158e+308
range -Infinity
or Infinity
is returned.parseFloat
converts BigInt
syntax to Numbers
, losing precision. This happens because the trailing n
character is discarded. Consider Number(value)
for stricter parsing, which converts to NaN
for arguments with invalid characters anywhere.
parseFloat
will parse non-string objects if they have a toString
or valueOf
method. The returned value is the same as if parseFloat
had been called on the result of those methods.
parseFloat
returning a numberThe following examples all return 3.14
:
parseFloat(3.14); parseFloat('3.14'); parseFloat(' 3.14 '); parseFloat('314e-2'); parseFloat('0.0314E+2'); parseFloat('3.14some non-digit characters'); parseFloat({ toString() { return "3.14" } });
parseFloat
returning NaN
The following example returns NaN
:
parseFloat('FF2');
Infinity values are returned when the number is outside the double-precision 64-bit IEEE 754-2019 format range:
parseFloat('1.7976931348623159e+308'); // Infinity parseFloat('-1.7976931348623159e+308'); // -Infinity
The following examples both return 900719925474099300
, losing precision as the integer is too large to be represented as a float:
parseFloat(900719925474099267n); parseFloat('900719925474099267n');
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
parseFloat |
1 |
12 |
1 |
3 |
3 |
1 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat