How to convert a string to a number in JavaScript

Learn how to use JavaScript to convert strings to numbers


JavaScript provides a variety of methods to convert string values to numbers.

Best: use Number objects

I think the best way is to use Number objects in a non-constructor context (newKey words):

const count = Number('1234') //1234

This will also take care of decimals.

Number is aPackaging objectMany operations can be performed. If we use the constructor (new Number("1234")), it will return a Number object instead of a numeric value, so please pay attention.

Number vs new Number

Pay attention to the separator between numbers:

Number('10,000') //NaN
Number('10.00') //10
Number('10000') //10000

If you need to use the decimal separator to parse the string, useIntl.NumberFormatinstead.

Other solutions

useparseInt()withparseFloat()

Another good solution for integers is to callparseInt()Features:

const count = parseInt('1234', 10) //1234

Don't forget the second parameter, the base, the decimal number is always 10, otherwise the conversion may try to guess the base and give unexpected results.

parseInt()Try to get numbers from strings that don't only contain numbers:

parseInt('10 lions', 10) //10

But if the string does not start with a number, you will getNaN(Not a number):

parseInt("I'm 10", 10) //NaN

In addition, just like Number, it is not reliable to use separators between numbers:

parseInt('10,000', 10) //10     ❌
parseInt('10.00', 10) //10     ✅ (considered decimals, cut)
parseInt('10.000', 10) //10     ✅ (considered decimals, cut)
parseInt('10.20', 10) //10     ✅ (considered decimals, cut)
parseInt('10.81', 10) //10     ✅ (considered decimals, cut)
parseInt('10000', 10) //10000  ✅

If you want to keep the fractional part instead of just the integer part, useparseFloat(). Please note that withparseInt()At the same level, it only requires one parameter-the string to be converted:

parseFloat('10,000')  //10     ❌
parseFloat('10.00')   //10     ✅ (considered decimals, cut)
parseFloat('10.000')  //10     ✅ (considered decimals, cut)
parseFloat('10.20')   //10.2   ✅ (considered decimals)
parseFloat('10.81')   //10.81  ✅ (considered decimals)
parseFloat('10000')   //10000  ✅

use+

One "trick" is to use unary operators+Before the string:

+'10,000'  //NaN ✅
+'10.000'  //10 ✅
+'10.00' //10 ✅
+'10.20'  //10.2 ✅
+'10.81'  //10.81 ✅
+'10000' //10000 ✅

See how it returnsNaNIn the first example, this is the correct behavior: it is not a number.

useMath.floor()

Similar to+Unary operator, but returns the integer part, usedMath.floor():

Math.floor('10,000') //NaN ✅
Math.floor('10.000') //10 ✅
Math.floor('10.00') //10 ✅
Math.floor('10.20') //10 ✅
Math.floor('10.81') //10 ✅
Math.floor('10000') //10000 ✅

use* 1

Usually one of the fastest options, which behaves like+Unary operator, so if the number is a floating point type, no conversion to an integer will be performed.

'10,000' * 1 //NaN ✅
'10.000' * 1 //10 ✅
'10.00' * 1 //10 ✅
'10.20' * 1 //10.2 ✅
'10.81' * 1 //10.81 ✅
'10000' * 1 //10000 ✅

which performed

Each of these methods has different performance in different environments, depending on the implementation. In my case* 1It is a wise choice for the winner, 10 times faster than other alternative products.

useJSPerftry it:

Performance in conversion

Download mine for freeJavaScript beginner's manual


More js tutorials: