parseFloat() is a JavaScript global function that parses a string and returns a floating-point number. It reads the string from the beginning, extracting numeric characters until it encounters a non-numeric character, then stops and returns the parsed number. If the first non-whitespace character cannot form a valid number, it returns NaN (Not a Number).
Syntax:
parseFloat(string)Input: A string (or value coerced to a string).
Output: A floating-point number, or NaN if parsing fails.
Key behavior: Ignores leading whitespace, stops at the first invalid character, and supports decimal points, scientific notation (
eorE), andInfinity/-Infinitywhen specified.
Examples
parseFloat("3.14"); // Returns 3.14
parseFloat(" 42.5 "); // Returns 42.5 (ignores spaces)
parseFloat("123abc"); // Returns 123 (stops at 'a')
parseFloat("abc123"); // Returns NaN (first char invalid)
parseFloat("1.7976931348623159e+308"); // Returns InfinityUsing with toFixed() for 2 Decimal Places
To format a parsed float to 2 decimal places, combine parseFloat() with toFixed(2):
let num = parseFloat("10.547892");
console.log(num.toFixed(2)); // Returns "10.55" (as a string)Note:
toFixed()returns a string. UseparseFloat()again if you need a number.
parseFloat() vs Number()
parseFloat()is more lenient: it ignores trailing invalid characters.Number()returns NaN if the entire string isn’t a valid number (e.g.,Number("123abc")→NaN, whileparseFloat("123abc")→123).
Number.parseFloat() (Alternative)
Number.parseFloat() is equivalent to the global parseFloat() and is preferred in modern code for clarity and consistency.
Note:
parseFloat()does not support non-decimal literals like0x(hex),0b(binary), or0o(octal).
The internal workings are not that different, as @James Allardic already answered. There is a difference though. Using parseFloat, a (trimmed) string starting with one or more numeric characters followed by alphanumeric characters can convert to a Number, with Number that will not succeed. As in:
parseFloat('3.23abc'); //=> 3.23
Number('3.23abc'); //=> NaN
In both conversions, the input string is trimmed, by the way:
parseFloat(' 3.23abc '); //=> 3.23
Number(' 3.23 '); //=> 3.23
Answer from KooiInc on Stack OverflowWhat is the difference between Number(...) and parseFloat(...)
When does parseFloat decide to round?
javascript - parseFloat(number2) returning int
Newest 'parsefloat' Questions - Stack Overflow
Videos
The internal workings are not that different, as @James Allardic already answered. There is a difference though. Using parseFloat, a (trimmed) string starting with one or more numeric characters followed by alphanumeric characters can convert to a Number, with Number that will not succeed. As in:
parseFloat('3.23abc'); //=> 3.23
Number('3.23abc'); //=> NaN
In both conversions, the input string is trimmed, by the way:
parseFloat(' 3.23abc '); //=> 3.23
Number(' 3.23 '); //=> 3.23
No. Both will result in the internal ToNumber(string) function being called.
From ES5 section 15.7.1 (The Number Constructor Called as a Function):
When
Numberis called as a function rather than as a constructor, it performs a type conversion...Returns a Number value (not a Number object) computed by
ToNumber(value)if value was supplied, else returns+0.
From ES5 section 15.1.2.3 (parseFloat (string)):
... If neither
trimmedStringnor any prefix oftrimmedStringsatisfies the syntax of aStrDecimalLiteral(see 9.3.1) ...
And 9.3.1 is the section titled "ToNumber Applied to the String Type", which is what the first quote is referring to when it says ToNumber(value).
Update (see comments)
By calling the Number constructor with the new operator, you will get an instance of the Number object, rather than a numeric literal. For example:
typeof new Number(10); //object
typeof Number(10); //number
This is defined in section 15.7.2 (The Number Constructor):
When
Numberis called as part of anewexpression it is a constructor: it initialises the newly created object.
I'm using parseFloat and am a bit confused about when it decides to round.
parseFloat("34.799999999999997") returns 34.8 parseFloat("34.79") returns 34.79
Why is the first rounded to one decimal place and the second is left alone?
This looks like a locale problem: parseFloat only recognizes a period as a decimal point; it stops parsing when it gets to the comma, giving you only integer values. Unfortunately, there is no way to change this behavior. You need to replace the commas with periods in your number strings in order to get a decimal number.
If you will use dot instead of comma(Example: 71.13 instead of 71,13) everything will work as expected