简体   繁体   English

Actionscript 将字符串转换为整数

[英]Actionscript Convert String to Int

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 2541

  • 4812 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 中非常有效。

In AS3, you can try:在 AS3 中,您可以尝试:

parseInt('04755', 10)

10 above is the radix .上面的 10 是基数

parseInt(yourString);

...is the correct answer. ...是正确的答案。 .parseInt() is a top-level function. .parseInt()是顶级 function。

Converting a string with a leading 0 to a Number in ActionScript 2 assumes that the number you want is octal .将带有前导 0 的字符串转换为 ActionScript 2 中的数字假定您想要的数字是octal Give this function I've made for you a try:试试我为你做的这个 function:

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() function 中,而不是像通常那样使用int()Number()

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM