JavaScript provides two methods for converting non-number primitives into numbers: parseInt() and parseFloat() . As you may have guessed, the former converts a value into an integer whereas the latter converts a value into a floating-point number.

Any number literal contained in a string is also converted correctly, so the string "0xA" is properly converted into the number 10. However, the string "22.5" will be converted to 22 , because the decimal point is an invalid character for an integer. Some examples:

var iNum1 = parseInt("1234blue"); //returns 1234

var iNum2 = parseInt("0xA"); //returns 10

var iNum3 = parseInt("22.5"); //returns 22

var iNum4 = parseInt("blue"); //returns NaN

The parseInt() method also has a radix mode, allowing you to convert strings in binary, octal, hexadecimal, or any other base into an integer. The radix is specified as a second argument to parseInt() , so a call to parse a hexadecimal value looks like this:

var iNum1 = parseInt("AF", 16); //returns 175

Of course, this can also be done for binary, octal, and even decimal (which is the default mode):

var iNum1 = parseInt("10", 2); //returns 2

var iNum2 = parseInt("10", 8); //returns 8

var iNum2 = parseInt("10", 10); //returns 10

If decimal numbers contain a leading zero, it’s always best to specify the radix as 10 so that you won’t accidentally end up with an octal value. For example:

var iNum1 = parseInt("010"); //returns 8

var iNum2 = parseInt("010", 8); //returns 8

var iNum3 = parseInt("010", 10); //returns 10

In this code, both lines are parsing the string "010" into a number. The first line thinks that the string is an octal value and parses it the same way as the second line (which specifies the radix as 8). The last line specifies a radix of 10, so iNum3 ends up equal to 10.

Another difference when using parseFloat() is that the string must represent a floating-point number in decimal form, not octal or hexadecimal. This method ignores leading zeros, so the octal number 0908 will be parsed into 908 , and the hexadecimal number 0xA will return NaN because x isn’t a valid character for a floating-point number. There is also no radix mode for parseFloat() .

Some examples of using parseFloat() :

var fNum1 = parseFloat("1234blue"); //returns 1234

var fNum2 = parseFloat("0xA"); //returns 0

var fNum3 = parseFloat("22.5"); //returns 22.5

var fNum4 = parseFloat("22.34.5"); //returns 22.34

var fNum5 = parseFloat("0908"); //returns 908

var fNum6 = parseFloat("blue"); //returns NaN

Read More, Read More

Similar Question Read more here

Answer from Techie on Stack Overflow
Top answer
1 of 2
57

JavaScript provides two methods for converting non-number primitives into numbers: parseInt() and parseFloat() . As you may have guessed, the former converts a value into an integer whereas the latter converts a value into a floating-point number.

Any number literal contained in a string is also converted correctly, so the string "0xA" is properly converted into the number 10. However, the string "22.5" will be converted to 22 , because the decimal point is an invalid character for an integer. Some examples:

var iNum1 = parseInt("1234blue"); //returns 1234

var iNum2 = parseInt("0xA"); //returns 10

var iNum3 = parseInt("22.5"); //returns 22

var iNum4 = parseInt("blue"); //returns NaN

The parseInt() method also has a radix mode, allowing you to convert strings in binary, octal, hexadecimal, or any other base into an integer. The radix is specified as a second argument to parseInt() , so a call to parse a hexadecimal value looks like this:

var iNum1 = parseInt("AF", 16); //returns 175

Of course, this can also be done for binary, octal, and even decimal (which is the default mode):

var iNum1 = parseInt("10", 2); //returns 2

var iNum2 = parseInt("10", 8); //returns 8

var iNum2 = parseInt("10", 10); //returns 10

If decimal numbers contain a leading zero, it’s always best to specify the radix as 10 so that you won’t accidentally end up with an octal value. For example:

var iNum1 = parseInt("010"); //returns 8

var iNum2 = parseInt("010", 8); //returns 8

var iNum3 = parseInt("010", 10); //returns 10

In this code, both lines are parsing the string "010" into a number. The first line thinks that the string is an octal value and parses it the same way as the second line (which specifies the radix as 8). The last line specifies a radix of 10, so iNum3 ends up equal to 10.

Another difference when using parseFloat() is that the string must represent a floating-point number in decimal form, not octal or hexadecimal. This method ignores leading zeros, so the octal number 0908 will be parsed into 908 , and the hexadecimal number 0xA will return NaN because x isn’t a valid character for a floating-point number. There is also no radix mode for parseFloat() .

Some examples of using parseFloat() :

var fNum1 = parseFloat("1234blue"); //returns 1234

var fNum2 = parseFloat("0xA"); //returns 0

var fNum3 = parseFloat("22.5"); //returns 22.5

var fNum4 = parseFloat("22.34.5"); //returns 22.34

var fNum5 = parseFloat("0908"); //returns 908

var fNum6 = parseFloat("blue"); //returns NaN

Read More, Read More

Similar Question Read more here

2 of 2
12

First of all only parseInt accepts second argument. It's called radix. It represents numeral system to be used. In example you can convert number into binary or hexadecimal code.

parseFloat only accepts one argument.

🌐
Medium
medium.com › @roy.elaawar › parsefloat-vs-parstint-in-javascript-4f3d345f205f
ParseFloat vs ParseInt in JavaScript | by Roy Elaawar | Medium
November 6, 2023 - ... The output is: result 123.22vs const result = parseInt('123.22 is a number');The output is: result 123 · The main difference is that parseInt provides us with a whole number.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseFloat
parseFloat() - JavaScript | MDN
A floating point number parsed ... 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....
🌐
Quora
quora.com › What-is-the-difference-between-parseInt-and-parseFloat-in-JavaScript
What is the difference between parseInt() and parseFloat() in JavaScript? - Quora
Answer (1 of 4): In short answer, with [code ]parseInt()[/code] you can parses a string into Integer Number, and with [code ]parseFloat()[/code] you can parses a string into Floating Number. In detail answer is : 1. [code ]parseInt()[/code] [code ]parseInt()[/code] has 2 parameters you have to...
🌐
Zapier
community.zapier.com › featured-articles-65 › javascript-code-parsefloat-vs-parseint-16320
JavaScript Code: parseFloat VS parseInt | Zapier Community
June 17, 2022 - How to handle numerical strings in JavaScript Code steps. parseInt() returns the first integer (w/ decimals) parseFloat() returns the first number (w/o decimals) NOTE: Input Data are all treated as strings.
🌐
Alibaba Cloud
topic.alibabacloud.com › a › the-difference-between-number--parseint--and-parsefloat--in-js_1_11_30521894.html
The difference between number (), parseint (), and parsefloat () in JS
In other words, the first decimal ... between parseInt() and parseFloat() is: The first decimal point in the string parsed by parseFloat() is valid, while parseInt() will stop parsing when it encounters a decimal point, because the decimal point is not a valid number ...
Find elsewhere
🌐
Java-samples
java-samples.com › showtutorial.php
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
JavaScript provides two methods for converting non-number primitives into numbers: parseInt() and parseFloat() . As you may have guessed, the former converts a value into an integer whereas the latter converts a value into a floating-point number.
🌐
Bennadel
bennadel.com › blog › 2012-exploring-javascripts-parseint-and-parsefloat-functions.htm
Exploring Javascript's parseInt() And parseFloat() Functions
April 21, 2020 - Javascript's parseFloat() function is not quite as robust as the parseInt() function. It parses the incoming string into a floating point number rather than a decimal; but, it can only work with incoming values in base10 (decimal values).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript | MDN
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 ...
🌐
Bennadel
bennadel.com › blog › 3803-i-prefer-the-unary-plus-operator-over-parseint-and-parsefloat-when-coercing-strings-to-numbers-in-javascript.htm
I Prefer The Unary Plus (+) Operator Over parseInt() And ParseFloat() When Coercing Strings To Numbers In JavaScript
April 4, 2020 - Ben Nadel favors the unary plus operator (+) over the parseInt() and parseFloat() methods in JavaScript when trying to coerce String values into Number values. It's short; fails more consistently; and maps closer to his intent.
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.

🌐
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
The parseFloat() method parses a value as a string and returns the first number.
Top answer
1 of 2
10
  • Number.parseInt method (or just parseInt)

    • Ignores leading and trailing whitespace
    • Parses a leading number to an integer (not a floating point number)
    • Ignores invalid trailing data
    • Lets you set the base to use when interpreting the number
    • Will interpret text starting with 0x as hexadecimal, if another base was not provided
    • Returns NaN if the value could not be successfully parsed to an integer
  • Number.parseFloat method (or just parseFloat)

    • Similar to parseInt, except that it allows for a decimal part to be interpreted
    • Only parses to base-10
  • Number() function (or class?)

    • Similar to parseFloat, but does not allow trailing text
    • Will return 0 for an empty string or a string that only contains whitespace
    • It's not a class; when called without new, it returns a primitive number
  • the + operator

    • Basically the same as Number(), but in operator form.
  • eval()

    • Interprets and executes the given input as a JavaScript program.
    • Given the string "2", it will be interpreted as a numeric literal, and return that value since it's the result of the last expression in the program
    • Throws an error if the input was not a valid program.
  • JSON.parse()

    • Parses the textual data as JSON-serialized data.
    • If the data is valid, it creates the JavaScript objects/primitives that are represented by the data, and returns them.
    • If the data is invalid, it throws an error.
    • Given the string "2", it will be interpreted as a numeric literal, and return the value that was successfully parsed out of it according to the parsing requirements of JSON.

So you decide which is appropriate to use based on their capabilities.

2 of 2
4

Number.parseInt() calls the global function parseInt() in the background, same with Number.parseFloat() see: Number.parseInt ECMA and Number.parseFloat ECMA

The calls Number("2") and "+2" is identical in the background, they both call ToNumber see: Number and Unary + Operator

When you know what types you are working with, or want a guaranteed type back, use parseFloat and parseInt, otherwise it tends to be easier to only use Number() as it will work within all your calculations, many people choose to use the unary + operator because they find it more pleasing to read/type, but that is only based on preference as it is identical to Number().

Also, when you using parseInt(), you can specify a radix, which is useful in certain applications where you want to work in different number systems, which you cannot do with Number()

If the ECMA standard references does not explain the details for you enough, I will add a summary for you.

🌐
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.
🌐
Cullen Web Services
cullenwebservices.com › home › javascript numbers: number(), parseint(), parsefloat()
JavaScript Numbers: Number(), parseInt(), parseFloat() - Cullen Web Services
December 11, 2015 - parseInt("10",16); //hexadecimal 16 parseInt("10",8); //octal 8 parseInt("10",10); //decimal 10 · parseFloat(true); //NaN parseFloat(false); //NaN parseFloat(13); //13 parseFloat("13"); //13 var j; //undefined parseFloat(j); //NaN var b=10; parseFloat(b) //10 parseFloat("1.5") //1.5 parseFloat("01.5") //1.5 parseFloat("0xA") //0 only uses decimal system parseFloat("") //NaN parseFloat("cindy"); //NaN parseFloat("123cindy") //123.123 parseFloat("4.89e7"); //48900000 ·