CAST TO NUMBER IN JAVASCRIPT USING THE UNARY (+) OPERATOR

Learn how to lớn convert a string lớn a number using JavaScript

Published May 14, 2018, Last Updated Oct 24, 2019


JavaScript provides various ways to lớn convert a string value inkhổng lồ a number.

Bạn đang xem: Cast to number in javascript using the unary (+) operator

Best: use the Number object

The best one in my opinion is to use the Number object, in a non-constructor context (without the new keyword):


This takes care of the decimals as well.

Number is a wrapper object that 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.

*

Watch out for separators between digits:


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

Xem thêm: Tải Game Vua Hải Tặc H5 - Cho Điện Thoại Android, Ios, Apk

Other solutions

Use parseInt() & parseFloat()

Another good solution for integers is to lớn hotline the parseInt() function:


Don’t forget the second parameter, which is the radix, always 10 for decimal numbers, or the conversion might try to lớn guess the radix and give sầu unexpected results.

parseInt() tries lớn get a number from a string that does not only contain a number:


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 khổng lồ retain the decimal part & not just get the integer part, use parseFloat(). cảnh báo that unlikeits parseInt() sibling, it only takes one argument – the string to convert:


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 ✅

+'10,000' //NaN ✅+'10.000' //10 ✅+'10.00' //10 ✅+'10.20' //10.2 ✅+'10.81' //10.81 ✅+'10000' //10000 ✅
See how it returns NaN in the first example, which is the correct behavior: it’s not a number.

Use Math.floor()

Similar khổng lồ the + unary operator, but returns the integer part, is khổng lồ use Math.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

Generally one of the faskiểm tra options, behaves lượt thích the + unary operator, so it does not perform conversion lớn an integer if the number is a float.


'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 ✅

Performance

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

Use JSPerf to try yourself:

*


The 2021 JavaScript Full-Stack Bootcamp will start at the kết thúc of March 2021. Don"t miss this opportunity, signup to lớn the waiting list!

More js tutorials:


*
Free ebooks