![]() If the performance is critical for your application and you are transforming huge amount of data, I would go for the unary operator(+) or multiply(*1). It’s according to your preference and views to decide which one you want to go for. ConclusionĪs you already saw, there is a lot of different ways to convert string to number in JavaScript. The situation here is similar, but the decimal part of the numbers is also retained, not just the whole number. ParseFloat('fdsfs111') // NaN(not a number) If you want to keep the decimal part, use the parseFloat(). And one more thing, the empty string mentioned a little bit earlier is NOT valid while the previous methods were converting it to 0. However, if the string starts with invalid digit, like ‘fdsfs111’, the result is NaN. The other important difference is that even if the string is not a valid number, like ‘-1.111dfdfd’, it takes the valid part and convert it into an integer. The most obvious and expected thing is that it cuts the decimal number and turns everything into an integer(whole number). ParseInt('fdsfs111') // NaN(not a number)Īs you can see from the example above, this way of converting has some differences from the previous ones. Radix – 10 by default which is what we need for our case, but you can change it (optional).In case you are working with integers only, parseInt() could be a good choice and of course parseFloat() when working with decimal numbers.īoth parseInt and parseFloat accept two parameters as arguments: ParseInt / ParseFloatĪ different solution with different behavior. Keep in mind that unless your have a millions of records that need to be converted from string to number, the speed shouldn’t be critical. Multiplying by one have the same behavior like the unary operator, but it is a little bit faster. +'-1,111' // NaN (not a number) Multiply by 1 (* 1) – FASTEST The behavior is the same as using the Number() one mentioned above. It is less popular which can cause readability problems while someone is reading/refactoring your code. Unary Operator (+)Īnother cool method is to use the unary operator + before the string. Have in mind that using comma as a decimal separator, will also result in NaN while providing empty string as input will result in 0, not NaN. An important thing to mention here – if the value(the string) can’t be converted to legal number, NaN(Not a number) will be returned. This method works for both integer and decimal numbers. Probably the best method to convert string to number is the Number() function. ![]() Here are the most popular methods to transform string to number using vanilla JavaScript: Number However, it could be a little confusing due to the fact that some of them will return a different result than the others. I’m saying “some”, because there are quite a few ways to achieve this. Converting string to number in JavaScript is easily achievable using some of the built in methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |