Are there are any side effects of using this method to convert a string to an integer
Are there any side effects if i convert a string to a number like below..
var numb=str*1;
If I check with the below code it says this is a number..
var str="123";
str=str*1;
if(!isNaN(str))
{
alert('Hello');
}
Please let me know if there are any concerns in using this method..
Solution 1:
When you use parseFloat
, or parseInt
, the conversion is less strict. 1b5
-> 1.
Using 1*number
or +number
to convert will result in NaN
when the input is not valid number. Though unlike parseInt
, floating point numbers will be parsed correctly.
Table covering all possible relevant options.
//Variables // parseInt parseFloat + 1* /1 ~~ |0 ^1 >>0 >>>0
var a = '123,',// 123 123 NaN 0 & <<0 0
b = '1.e3',// 1 1000 1000 1000 1000
c = '1.21',// 1 1.21 1.21 1 1
d = '0020',// 16 20 20 20 20
e = '0x10',// 16 0 16 16 16
f = '3e9', // 3 3000000000 <-- -1294967296 3000000000
g = '3e10',// 3 30000000000 <-- -64771072 4230196224
h = 3e25 ,// 3 3e+25 3e+25 0 0
i = '3e25',// 3 3e+25 3e+25 0 0
j = 'a123',// NaN NaN NaN 0 0
k = ' 1 ',// 1 1 1 1 1
l = ' ',// NaN NaN 0 0 0
m = '.1 ',// NaN 0.1 0.1 1 1
n = '1. ',// 1 1 1 1 1
o = '1e999',// 1 Infinity Infinity 0 0
p = '1e-999',// 1 0 0 0 0
q = false ,// NaN NaN 0 0 0
r = void 0,// NaN NaN NaN 0 0
_ = function(){return 1;}, /* Function _ used below */
s={valueOf:_},//NaN NaN 1 1 1
t={toString:_};// 1 1 1 1 1
// Intervals: (-1e+20, +1e20) (-∞,+∞) (-∞,+∞) (-2³¹,+2³¹) [0, 2³²)
// In FF9 and Chrome 17, Infinity === Math.pow(2, 1024), approx. 1.7976e+308
// In FF9 and Chrome 17, bitwise operators always return 0 after about ±1e+25
Notes on number conversion methods:
- The number conversion always fail if the first character, after trimming white-space, is not a number.
-
parseInt
returns an integer representation of the first argument. When the radix (second argument) is omitted, the radix depends on the given input.0_
= octal (base-8),0x_
= hexadecimal (base-16). Default: base-10.parseInt
ignores any non-digit characters, even if the argument was actually a number: See h, i.
To avoid unexpected results, always specify the radix, usually 10:parseInt(number, 10)
. -
parseFloat
is the most tolerant converter. It always interpret input as base-10, regardless of the prefix (unlikeparseInt
). For the exact parsing rules, see here.
The following methods will always fail to return a meaningful value if the string contains any non-number characters. (valid examples:1.e+0 .1e-1
) -
+n, 1*n, n*1, n/1
andNumber(n)
are equivalent. -
~~n, 0|n, n|0, n^1, 1^n, n&n, n<<0
andn>>0
are equivalent. These are signed bitwise operations, and will always return a numeric value (zero instead ofNaN
). -
n>>>0
is also a bitwise operation, but does not reserve a sign bit. Consequently, only positive numbers can be represented, and the upper bound is 232 instead of 231.
- When passed an object,
parseFloat
andparseInt
will only look at the.toString()
method. The other methods first look for.valueOf()
, then.toString()
. See q - t.
-
NaN
, "Not A Number":typeof NaN === 'number'
NaN !== NaN
. Because of this awkwardness, useisNaN()
to check whether a value isNaN
.
When to use which method?
-
parseFloat( x )
when you want to get as much numeric results as possible (for a given string). -
parseFloat( (x+'').replace(/^[^0-9.-]+/,'') )
when you want even more numeric results. -
parseInt( x, 10 )
if you want to get integers. -
+x, 1*x ..
if you're only concerned about getting true numeric values of a object, rejecting any invalid numbers (asNaN
). -
~~, 0| ..
if you want to always get a numeric result (zero for invalid). -
>>>0
if negative numbers do not exists.
The last two methods have a limited range. Have a look at the footer of the table.
The shortest way to test whether a given parameter is a real number is explained at this answer:
function isNumber(n) {
return typeof n == 'number' && !isNaN(n - n);
}