Javascript String 到 int 的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18713508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript String to int conversion
提问by Skytiger
I have the following JS immbedded in a page:
我在页面中嵌入了以下 JS:
var round = Math.round;
var id = $(this).attr("id");
var len = id.length;
var indexPos = len -1; // index of the number so that we can split this up and used it as a title
var pasType = id.substring(0, indexPos); // adult, child or infant
var ind = round(id.substring(indexPos)); // converts the string index to an integer
var number = (id.substring(indexPos) + 1); // creates the number that will go in the title
window.alert(number);
id
will be something like adult0, and I need to take that string and split it into adult and 0 - this part works fine.
id
将类似于成人 0,我需要将该字符串拆分为成人和 0 - 这部分工作正常。
The problem comes in when I try to increment the 0. As you can see I use Math.round to convert it to an integer, and then add 1 to it - I expect 0 to be 1 after this. However, it doesn't seem to be converting it to integer, because I get 01
, not 1
. When testing this with adult1 the alert I get is 11
.
当我尝试增加 0 时,问题就出现了。如您所见,我使用 Math.round 将其转换为整数,然后将其加 1 - 我希望在此之后 0 为 1。但是,它似乎并没有将其转换为整数,因为我得到的是01
,而不是1
. 当用 Adult1 测试这个时,我得到的警报是11
.
I'm using thisquestion for reference, and have also tried var number += id.substring(indexPos);
, which breaks the JS (unexpected identifier '+='
)
我正在使用这个问题作为参考,并且也尝试过var number += id.substring(indexPos);
,这打破了 JS( unexpected identifier '+='
)
Does anyone know what I'm doing wrong? Is there a better way of doing this?
有谁知道我做错了什么?有没有更好的方法来做到这一点?
回答by Charaf JRA
回答by Qantas 94 Heavy
This is to do with JavaScript's +
in operator - if a number and a string are "added" up, the number is converted into a string:
这与 JavaScript 的+
in 运算符有关 - 如果数字和字符串“相加”,则数字将转换为字符串:
0 + 1; //1
'0' + 1; // '01'
To solve this, use the +
unary operator, or use parseInt()
:
要解决此问题,请使用+
一元运算符,或使用parseInt()
:
+'0' + 1; // 1
parseInt('0', 10) + 1; // 1
The unary +
operator converts it into a number (however if it's a decimal it will retain the decimal places), and parseInt()
is self-explanatory (converts into number, ignoring decimal places).
一元运算+
符将其转换为数字(但是如果它是小数,它将保留小数位),并且parseInt()
不言自明(转换为数字,忽略小数位)。
The second argument is necessary for parseInt()
to use the correct base when leading 0s are placed:
parseInt()
当放置前导 0 时,第二个参数对于使用正确的基数是必需的:
parseInt('010'); // 8 in older browsers, 10 in newer browsers
parseInt('010', 10); // always 10 no matter what
There's also parseFloat()
if you need to convert decimals in strings to their numeric value - +
can do that too but it behaves slightly differently: that's another story though.
还有parseFloat()
,如果你需要在小数字符串转换为自己的数值-+
可以做到这一点,但它的行为稍有不同:这是另一回事,虽然。
回答by Jite
JS will think that the 0 is a string, which it actually is, to convert it to a int, use the: parseInt() function, like:
JS 会认为 0 是一个字符串,它实际上是一个字符串,要将其转换为 int,请使用: parseInt() 函数,例如:
var numberAsInt = parseInt(number, 10);
// Second arg is radix, 10 is decimal.
If the number is not possible to convert to a int, it will return NaN, so I would recommend a check for that too in code used in production or at least if you are not 100% sure of the input.
如果数字无法转换为 int,它将返回 NaN,因此我建议在生产中使用的代码中也进行检查,或者至少如果您不是 100% 确定输入。
回答by Arshid KV
Convert by Number Class
:-
转换方式Number Class
:-
Eg:
例如:
var n = Number("103");
console.log(n+1)
Output:104
输出:104
Note:-Number
is class. When we pass string, then constructor of Number class
will convert it.
注意:-Number
是类。当我们传递字符串时,构造函数Number class
将转换它。
回答by Alexander Jank
Although parseInt
is the official function to do this, you can achieve the same with this code:
虽然这parseInt
是执行此操作的官方功能,但您可以使用以下代码实现相同的功能:
number*1
The advantage is that you save some characters, which might save bandwidth if your code has to lots of such conversations.
优点是您可以保存一些字符,如果您的代码需要进行大量此类对话,这可能会节省带宽。
回答by juju
Use parseInt():
使用 parseInt():
var number = (parseInt(id.substring(indexPos)) + 1);` // creates the number that will go in the title
回答by user1203684
If you are sure id.substring(indexPos) is a number, you can do it like so:
如果你确定 id.substring(indexPos) 是一个数字,你可以这样做:
var number = Number(id.substring(indexPos)) + 1;
Otherwise I suggest checking if the Number function evaluates correctly.
否则我建议检查 Number 函数是否正确评估。