Our volunteers haven't translated this article into Ελληνικά yet. Join us and help get the job done!
You can also read the article in English (US).
The parseFloat()
function parses an argument and returns a floating point number.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
parseFloat(value)
Parameters
value
- The value you want to parse.
Return value
A floating point number parsed from the given value. If the value cannot be converted to a number, NaN
is returned.
Description
parseFloat
is a top-level function and is not associated with any object.
parseFloat
parses its argument, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed. Consider Number(value)
for stricter parsing, which converts to NaN
for values with such invalid characters regardless of position.
If the value is a string and the first character cannot be converted to a number, parseFloat
also returns NaN
.
For arithmetic purposes, the NaN
value is not a number in any radix. You can call the isNaN
function to determine if the result of parseFloat
is NaN
. If NaN
is passed on to arithmetic operations, the operation results will also be NaN
.
parseFloat
can also parse and return the value Infinity
. You can use the isFinite
function to determine if the result is a finite number (not Infinity
, -Infinity
, or NaN
).
parseFloat
is also able to parse an object if it has a toString
or valueOf
method defined. The returned value is the same as if parseFloat
had been called on the result of calling that method.
Examples
parseFloat
returning a number
The following examples all return 3.14
:
parseFloat(3.14); parseFloat('3.14'); parseFloat('314e-2'); parseFloat('0.0314E+2'); parseFloat('3.14more non-digit characters'); var foo = Object.create(null); foo.toString = function () { return "3.14"; }; parseFloat(foo); var foo = Object.create(null); foo.valueOf = function () { return "3.14"; }; parseFloat(foo);
parseFloat
returning NaN
The following example returns NaN
:
parseFloat('FF2');
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) The definition of 'parseFloat' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'parseFloat' in that specification. |
Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'parseFloat' in that specification. |
Draft |
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Basic support | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Legend
- Full support
- Full support