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 - In summary, use parseInt when you want to convert a string to an integer, and use parseFloat when you want to convert a string to a floating-point number.
Discussions

The advantage of using Number() instead of parseInt or parseFloat
What is the advantage of using Number() instead of parseInt or parseFloat? More on sitepoint.com
🌐 sitepoint.com
1
May 30, 2023
parseInt vs parseFloat
Taylor Fry is having issues with: Why would you ever use parseInt? I tested it out and you can pass a whole integer to parseFloat and it will display it. Even if you pass it 3.00... More on teamtreehouse.com
🌐 teamtreehouse.com
2
February 4, 2017
What is the critical difference between 'Number.parseInt()', 'Number.parseFloat()', 'Number()' or '+'?
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 ... More on stackoverflow.com
🌐 stackoverflow.com
Is there a reason to ever use parseInt when parseFloat does the same thing?
Ryan Schmelter is having issues with: parseFloat will convert an string like "100" into a number. Just wondering why we shouldn't use that all the time. More on teamtreehouse.com
🌐 teamtreehouse.com
3
June 15, 2017
🌐
Zapier
community.zapier.com › home › learn and share › featured articles › javascript code: parsefloat vs parseint
JavaScript Code: parseFloat VS parseInt | Zapier Community
June 17, 2022 - parseInt() returns the first integer (w/ decimals) parseFloat() returns the first number (w/o decimals) NOTE: Input Data are all treated as strings.
🌐
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...
🌐
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 - Both the parseInt() and the parseFloat() methods parse the input string until they hit a non-numeric value, at which point they discard the rest of the input. So, to be clear, this is parseInt() working exactly how it was designed to work. The problem is, this is almost certainly not what the original engineer intended.
🌐
SitePoint
sitepoint.com › javascript
The advantage of using Number() instead of parseInt or parseFloat
May 30, 2023 - Number will take a number-formatted string, and turn it into a numerical value of the appropriate type. parseInt will take any string that begins with a digit, and turn it into a flatform integer.
Find elsewhere
🌐
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 ...
🌐
YouTube
youtube.com › watch
JavaScript: ParseInt | ParseFloat | Difference between parseInt, parseFloat | Interview questions - YouTube
Explained about parseInt, parseFloat functions.#javascript #jstutorial #javascriptforbeginners #modernjavascript #learnjavascript
Published: October 19, 2020
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.

🌐
Copahost
copahost.com › home › javascript parsefloat: examples and variations of this function
Javascript parseFloat: Examples and variations of this function - Copahost
July 16, 2023 - Use parseFloat() when you need to extract a floating-point number that may contain decimal places. It will preserve the fractional part and return a floating-point number. Numeric Base: If you are parsing strings representing numbers in different bases, such as binary, octal, or hexadecimal, you should use parseInt() with the appropriate radix (base) parameter.
🌐
Bennadel
bennadel.com › blog › 2012-exploring-javascripts-parseint-and-parsefloat-functions.htm
Exploring Javascript's parseInt() And parseFloat() Functions
April 21, 2020 - Other than not treating "0x" as base16 or "012" as base8, the only significant difference is that parseFloat() returns decimals for values like "123.456" where as parseInt() would just return 123.
Top answer
1 of 3
2
They do different things. Sure, they both convert "100", but parseFloat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) will also convert something like "33.3". On the other hand, parseInt (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) will enforce acceptance of only integer input, and it will also take a second argument that allows you to specify a conversion radix other than decimal. What does the hex value "F00D" represent? parseInt("F00D", 16) will tell ya!
2 of 3
1
Hey there! I have found some discussions that cover the difference between using the two. https://stackoverflow.com/a/12813049/7932859 https://stackoverflow.com/a/9528452/7932859 Here are also the docs for the different usages. + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/GlobalObjects/parseInt + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/GlobalObjects/parseFloat parseInt() has an extra parameter called "radix" which is describes as radix: An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10. I hope this helps!
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 13473 › 0 › number-vs-vs-parsefloat-vs-parseint
Benchmark: Number vs + vs parseFloat vs parseInt - MeasureThat.net
parseFloat · 4.4M/s · Fastest: parseInt (128.8x faster) View exact numbers · ✓ parseInt: 568,072,576 ops/sec · • Unary +: 4,618,406 ops/sec · • Number: 4,550,124 ops/sec · • parseFloat: 4,411,436 ops/sec · Results show median performance across all community submissions.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseFloat
parseFloat() - JavaScript - MDN Web Docs
For example, parseInt("42") and parseFloat("42") would return the same value: a Number 42. The parseFloat function converts its first argument to a string, parses that string as a decimal number literal, then returns a number or NaN.
🌐
YouTube
youtube.com › watch
How to Model Data Efficiency: parseInt & parseFloat --The Modern JavaScript Bootcamp - YouTube
parseInt & parseFloat - javascript programming tutorial 16 - parseint and parsefloat methods. en este video vemos los métodos number parseint y parsefloat p...
Published: August 10, 2020
🌐
Jsperf
jsperf.app › parseint-vs-parsefloat
parseInt VS parseFloat
VERDICT! Use parseFloat, always, unless you're trying to actually grab the integer value out of a floating-point string.