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 (e or E), and Infinity/-Infinity when 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 Infinity

Using 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. Use parseFloat() 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, while parseFloat("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 like 0x (hex), 0b (binary), or 0o (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 Overflow
🌐
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
W3Schools.com
The parseFloat() method parses a value as a string and returns the first number.
Discussions

What is the difference between Number(...) and parseFloat(...)
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. More on stackoverflow.com
🌐 stackoverflow.com
When does parseFloat decide to round?
Welcome to the wonderful world of floating point numbers. More on reddit.com
🌐 r/javascript
13
1
October 20, 2014
javascript - parseFloat(number2) returning int
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. More on stackoverflow.com
🌐 stackoverflow.com
Newest 'parsefloat' Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
🌐
Codecademy
codecademy.com › docs › javascript › number methods › .parsefloat()
JavaScript | Number Methods | .parseFloat() | Codecademy
May 31, 2024 - In JavaScript, the .parseFloat() method parses a given string and returns the first floating-point number found in the string. Parsing stops when it encounters a character that is not part of a valid number.
Top answer
1 of 5
67

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
2 of 5
33

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 Number is 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 trimmedString nor any prefix of trimmedString satisfies the syntax of a StrDecimalLiteral (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 Number is called as part of a new expression it is a constructor: it initialises the newly created object.

🌐
Oracle
docs.oracle.com › cd › B40099_02 › books › eScript › eScript_JSReference199.html
Bookshelf v8.0: parseFloat() Method
Siebel eScript Language Reference > Siebel eScript Commands > Conversion Methods > · This method converts an alphanumeric string to a floating-point decimal number
🌐
Flexiple
flexiple.com › javascript › parsefloat-javascript
parseFloat JavaScript: Syntax and Examples - Flexiple
Learn how to use the JavaScript parseFloat function to convert strings to floating-point numbers accurately. Examples and tips included.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-number-parsefloat-method
JavaScript Number parseFloat() Method - GeeksforGeeks
July 11, 2025 - JavaScript parseFloat() Method is used to accept the string and convert it into a floating-point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript | MDN
July 10, 2025 - The Number.parseFloat() static method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN.
🌐
YouTube
youtube.com › steve griffith
How to Use parseInt and parseFloat - YouTube
This tutorial explains how you can use parseInt and parseFloat to extract numeric values from Strings, as well as, why you would want to do this. Sample Code...
Published   January 10, 2020
Views   3K
🌐
Edgecompute
js-compute-reference-docs.edgecompute.app › parsefloat()
parseFloat() | @fastly/js-compute
The parseFloat function converts its first argument to a string, parses that string as a decimal number literal, then returns a number or NaN.
🌐
Medium
medium.com › @maxheadway › the-differences-between-number-and-parsefloat-in-javascript-8ee74b961ed4
The differences between Number() and parseFloat() in Javascript | by Max Headway | Medium
February 19, 2023 - The parseFloat Function The parseFloat function is used to convert a string to a floating-point number. It parses the string argument and returns a floating-point number.
🌐
Vultr Docs
docs.vultr.com › javascript › global › parseFloat
JavaScript parseFloat() - Parse String to Float | Vultr Docs
November 6, 2024 - The parseFloat() function in JavaScript is a invaluable tool for transforming string data into float numbers, significantly aiding in calculations and data processing. It efficiently handles a variety of string formats including those with ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › float-parsefloat-method-in-java-with-examples
Float parseFloat() method in Java with examples - GeeksforGeeks
July 11, 2025 - The parseFloat() method in Float Class is a built in method in Java that returns a new float initialized to the value represented by the specified String, as done by the valueOf method of class Float.
🌐
Copahost
copahost.com › home › javascript parsefloat: examples and variations of this function
Javascript parseFloat: Examples and variations of this function - Copahost
July 16, 2023 - We can use the parseFloat method to convert a string into a floating-point number. The parseFloat method takes a string value and returns a floating-point number. But there are few rules for using parseFloat method in JavaScript.
🌐
Medium
medium.com › @roy.elaawar › parsefloat-vs-parstint-in-javascript-4f3d345f205f
ParseFloat vs ParseInt in JavaScript | by Roy Elaawar | Medium
November 6, 2023 - ParseFloat vs ParseInt in JavaScript Both of these functions are crucial for extracting numeric values from strings, and they each have their own unique characteristics and uses. parseFloat is a …
🌐
Reddit
reddit.com › r/javascript › when does parsefloat decide to round?
r/javascript on Reddit: When does parseFloat decide to round?
October 20, 2014 -

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?

Top answer
1 of 5
5
Welcome to the wonderful world of floating point numbers.
2 of 5
3
It's not that parseFloat() is doing any rounding. That's not how you should look at it. The way you should look at it is that neither 34.799999999999997 nor 34.8 can be represented exactly by binary floating point. The closest representable IEEE double is 34.7999999999999971578290569595992565155029296875. As you can see, that value lies in between 34.799999999999997 and 34.8. That is, if you ask, "What is the nearest representable value to 34.799999999999997?" the answer is "34.7999999999999971578290569595992565155029296875, which is slightly larger." Likewise, if you ask, "What is the nearest representable value to 34.8?" the answer is, "34.7999999999999971578290569595992565155029296875, which is slightly smaller." In other words, as far as IEEE binary floating point numbers are concerned, 34.799999999999997 and 34.8 are equivalent values. They are two different ways of referring to the same number, 34.7999999999999971578290569595992565155029296875. When given a choice between two representations for the same number, the smaller or more compact one is chosen. This is an implementation detail, but it's one that's commonly made. 34.79 also is impossible to represent exactly in IEEE binary floating point. The nearest representable IEEE double is 34.78999999999999914734871708787977695465087890625. So when you write 34.79, you're getting 34.78999999999999914734871708787977695465087890625. As before, when displaying a floating point number, the library routines try to find the smallest representation that still round-trips to the same number. That's why 34.79 is the result of printing 34.78999999999999914734871708787977695465087890625 and not 34.78999999999999914734871708787977695465087890625. As a thought experiment, we can undo this choice, however: parseFloat("34.799999999999997") == 34.7999999999999971578290569595992565155029296875 parseFloat("34.8" == 34.7999999999999971578290569595992565155029296875 parseFloat("34.79") == 34.78999999999999914734871708787977695465087890625 That's what's actually going on. Those are the real values that you're using. It's just that for display purposes, people don't want to deal with 34.7999999999999971578290569595992565155029296875. If two things both result in the same value, they are equivalent, and the smaller one can be used as a representation.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-parse-float-with-two-decimal-places-in-javascript
How to Parse Float with Two Decimal Places in JavaScript? - GeeksforGeeks
The parseFloat() method converts a string to a floating-point number. If the string isn't numeric, it returns NaN.
Published   July 23, 2025
🌐
Stack Overflow
stackoverflow.com › questions › tagged › parsefloat
Newest 'parsefloat' Questions - Stack Overflow
In my test, I need to parseFloat() a string. The issue is that hyphen-minus instantly occurs NaN.