string Actionscript 将字符串转换为 Int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6146770/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:07:35  来源:igfitidea点击:

Actionscript Convert String to Int

stringactionscriptint

提问by greg

I am using Actionscript 2.0

我正在使用 Actionscript 2.0

In a Brand new Scene. My only bit of code is:

在一个全新的场景中。我唯一的代码是:

    trace(int('04755'));
    trace(int('04812'));

Results in:

结果是:

  • 2541

  • 4812

  • 2541

  • 4812

Any idea what I am doing wrong/silly?

知道我做错了什么/愚蠢吗?

By the way, I am getting this source number from XML, where it already has the leading 0. Also, this works perfect in Actionscript 3.

顺便说一下,我是从 XML 获取这个源代码的,它已经有前导 0。此外,这在 Actionscript 3 中非常有效。

采纳答案by Marty

Converting a string with a leading 0 to a Number in ActionScript 2 assumes that the number you want is octal. Give this function I've made for you a try:

在 ActionScript 2 中将带有前导 0 的字符串转换为数字假定您想要的数字是八进制。试试我为你做的这个功能:

var val:String = '00010';

function parse(str:String):Number
{
    for(var i = 0; i < str.length; i++)
    {
        var c:String = str.charAt(i);
        if(c != "0") break;
    }

    return Number(str.substr(i));
}

trace(parse(val)); // 10
trace(parse(val) + 10); // 20

Basically what you want to do now is just wrap your string in the above parse()function, instead of int()or Number()as you would typically.

基本上,你现在要做的是只是包装上面的字符串parse()函数,而不是int()Number()作为您通常会。

回答by Rahul Singhai

In AS3, you can try:

在 AS3 中,您可以尝试:

parseInt('04755', 10)

10 above is the radix.

上面的10是基数

回答by Engineer

parseInt(yourString);

...is the correct answer. .parseInt()is a top-level function.

……是正确答案。.parseInt()是一个顶级函数。

回答by Jakir Hossen

Bit of a simple one...

有点简单的...

try this -

尝试这个 -

temp="120";
temp2="140";
temp3=int ( temp );
temp4=int ( temp2 );
temp5=temp4+temp3;
trace(temp5);

so, all you need is...

所以,你所需要的只是……

int("190");