Skip to main content

How to Cast String to Int Javascript

How to Cast String to Int Javascript


How to convert a string to a number in JavaScript

Larn how to convert a cord to a number using JavaScript

JavaScript provides various ways to convert a cord value into a number.

Best: employ the Number object

The all-time ane in my opinion is to utilise the Number object, in a not-constructor context (without the new keyword):

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

This takes care of the decimals as well.

Number is a wrapper object that tin can perform many operations. If we use the constructor (new Number("1234")) it returns us a Number object instead of a number value, so pay attention.

Number vs new Number

Watch out for separators betwixt digits:

            Number('10,000')              //NaN                            Number('ten.00')              //10                            Number('10000')              //10000                                    

In the case you need to parse a string with decimal separators, apply Intl.NumberFormat instead.

Other solutions

Use parseInt() and parseFloat()

Another good solution for integers is to call the parseInt() function:

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

Don't forget the 2d parameter, which is the radix, always 10 for decimal numbers, or the conversion might try to judge the radix and give unexpected results.

parseInt() tries to get a number from a string that does not just contain a number:

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

merely if the string does not start with a number, yous'll become NaN (Non a Number):

            parseInt("I'yard ten",              x)              //NaN                                    

As well, but similar Number information technology's not reliable with separators between the digits:

            parseInt('ten,000',              10)              //10     ❌                            parseInt('10.00',              10)              //ten     ✅ (considered decimals, cut)                            parseInt('ten.000',              10)              //10     ✅ (considered decimals, cutting)                            parseInt('ten.twenty',              ten)              //10     ✅ (considered decimals, cut)                            parseInt('10.81',              ten)              //10     ✅ (considered decimals, cut)                            parseInt('10000',              10)              //10000  ✅                                    

If you want to retain the decimal part and non just get the integer part, use parseFloat(). Note that different its parseInt() sibling, it only takes one argument – the string to convert:

            parseFloat('10,000')              //ten     ❌                            parseFloat('10.00')              //10     ✅ (considered decimals, cut)                            parseFloat('10.000')              //10     ✅ (considered decimals, cutting)                            parseFloat('x.20')              //10.2   ✅ (considered decimals)                            parseFloat('10.81')              //10.81  ✅ (considered decimals)                            parseFloat('10000')              //10000  ✅                                    

Use +

I "trick" is to employ the unary operator + earlier the string:

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

Encounter how information technology returns NaN in the first instance, which is the correct behavior: it'south non a number.

Use Math.floor()

Similar to the + unary operator, but returns the integer function, is to utilise Math.floor():

            Math.floor('x,000')              //NaN ✅                            Math.floor('10.000')              //10 ✅                            Math.floor('ten.00')              //ten ✅                            Math.floor('x.20')              //x ✅                            Math.flooring('10.81')              //ten ✅                            Math.floor('10000')              //10000 ✅                                    

Utilize * 1

Mostly i of the fastest options, behaves like the + unary operator, so information technology does not perform conversion to an integer if the number is a float.

                          '10,000'              *              1              //NaN ✅                                          '10.000'              *              ane              //10 ✅                                          '10.00'              *              1              //10 ✅                                          '10.twenty'              *              1              //10.two ✅                                          'x.81'              *              1              //10.81 ✅                                          '10000'              *              1              //10000 ✅                                    

Operation

Every one of these methods has a different performance on unlike environments, as information technology all depends on the implementation. In my instance, * 1 is the winner performance-wise 10x faster than other alternatives.

Use JSPerf to try yourself:

Performance in conversion

How to Cast String to Int Javascript

Posted by: morningsamplim.blogspot.com

Comments