🌐
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
JavaScript provides an inbuilt parseFloat() method to parse a string and returns a floating-point number.
Published   July 23, 2025
Discussions

Difference parseInt() and parseFloat()
Nadia . is having issues with: Hi! Can anyone explain me the difference between parseInt and parseFloat? Like in what kind of situation should you use each of them? More on teamtreehouse.com
🌐 teamtreehouse.com
3
June 8, 2016
Converting a string to a float in Javascript?
+"3.4" => 3.4 More on reddit.com
🌐 r/javascript
15
0
March 27, 2015
javascript - What is the difference between Number(...) and parseFloat(...) - Stack Overflow
5 Why parseFloat in javascript returns string type for me? 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
🌐
Programiz
programiz.com › javascript › library › built-in › parseFloat
JavaScript parseFloat()
Become a certified JavaScript programmer. Try Programiz PRO! ... The parseFloat() function parses an argument and returns a floating-point number.
🌐
Scaler
scaler.com › home › topics › javascript parsefloat()
JavaScript parseFloat() Method - Scaler Topics
March 7, 2024 - Learn how to use the parseFloat() method in JavaScript to convert strings to floating-point numbers efficiently. Master concept of parsefloat in JavaScript.
🌐
Flexiple
flexiple.com › javascript › parsefloat-javascript
parseFloat JavaScript: Syntax and Examples - Flexiple
March 14, 2022 - Learn how to use the JavaScript parseFloat function to convert strings to floating-point numbers accurately. Examples and tips included.
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
❮ Previous JavaScript Global ... was 40"); Try it Yourself » · More examples below. The parseFloat() method parses a value as a string and returns the first number....
🌐
Edgecompute
js-compute-reference-docs.edgecompute.app › parsefloat()
parseFloat() | @fastly/js-compute
The parseFloat() function parses a string argument and returns a floating point number. ... 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 ...
🌐
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
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.
🌐
Reddit
reddit.com › r/javascript › converting a string to a float in javascript?
r/javascript on Reddit: Converting a string to a float in Javascript?
March 27, 2015 -

I can't use parseFloat(). I am so far doing the obvious by checking each character, but I feel there has to be a more elegant way! My function needs to act exactly the same as parseFloat() does. Can anyone help?

🌐
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.
🌐
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 ...
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.

🌐
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.
🌐
MDN
mdn2.netlify.app › en-us › docs › web › javascript › reference › global_objects › parsefloat
parseFloat() - JavaScript | MDN
February 18, 2022 - The parseFloat() function parses an argument (converting it to a string first if needed) and returns a floating point number.
🌐
GitHub
github.com › mdn › content › blob › main › files › en-us › web › javascript › reference › global_objects › number › parsefloat › index.md
content/files/en-us/web/javascript/reference/global_objects/number/parsefloat/index.md at main · mdn/content
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 {{jsxref("NaN")}}. ... - : The value to parse, [coerced to a string](/en-US/doc...
Author   mdn
🌐
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.