使用此方法将字符串转换为整数是否有任何副作用 [英] 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..
推荐答案
当您使用 parseFloat
或 parseInt时
,转换不太严格。 1b5
- > 1.
When you use parseFloat
, or parseInt
, the conversion is less strict. 1b5
-> 1.
使用 1 *号
当输入无效时,要转换的 + number
将导致 NaN
。虽然与 parseInt
不同,但浮点数将被正确解析。
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.
//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
关于数字转换方法的注意事项:
- 如果第一个字符在修剪空白后,数字转换总是会失败,不是数字。
-
parseInt
返回第一个参数的整数表示。当省略基数(第二个参数)时,基数取决于给定的输入。
0 _
=八进制(base-8),0x _
=十六进制(base-16)。默认值:base-10。
parseInt
忽略任何非数字字符,即使参数实际上是数字:参见 h,我。
为避免意外结果,请始终指定基数,通常为10:parseInt(number,10)
。 -
parseFloat
是最宽容的转换器。无论前缀如何,它总是将输入解释为base-10(与parseInt
不同)。有关确切的解析规则,请参阅此处。
如果字符串包含任何非数字字符,以下方法将始终无法返回有意义的值。(有效示例:1.e + 0 .1e-1
) -
+ n,1 * n,n * 1,n / 1
和Number(n)
是等价的。 -
~~ n,0 | n,n | 0,n ^ 1,1 n,n& n,n<< 0
和n>> 0
是等价的。这些是按位符号操作,并将始终返回一个数值(零而不是NaN
)。 -
n>>> 0
也是按位操作,但不保留符号位。因此,只能表示正数,上限为2 32 而不是2 31 。
- 传递对象时,
parseFloat
和parseInt
只会查看.toString()
方法。其他方法首先查找.valueOf()
,然后.toString()
。 参见 q - t 。
-
NaN
,不是数字:
typeof NaN ==='number'
NaN!== NaN
。由于这种尴尬,请使用isNaN()
检查值是否为NaN
。 - 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
.-
parseFloat(x)
何时需要获得尽可能多的数字结果(对于给定的字符串)。 -
parseFloat((x +'')。replace(/ ^ [^ 0-9。 - ] + /,''))
当你想要更多的数字结果时。 -
parseInt(x,10)
如果你想得到整数。 -
+ x,1 * x ..
如果你只关心获取对象的真实数值,拒绝任何无效数字(如NaN
)。 -
~~, 0 | ..
如果你想总是得到一个数字结果(零无效)。 -
>>> 0
如果不存在负数。
最后两种方法的范围有限。看一下表格的页脚。 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.
Notes on number conversion methods:
测试给定参数是否为实数在 此答案 :
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);
}
这篇关于使用此方法将字符串转换为整数是否有任何副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!