10

I am using Actionscript 2.0

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

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

Results in:

  • 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.

4 Answers 4

30

In AS3, you can try:

parseInt('04755', 10)

10 above is the radix.

Sign up to request clarification or add additional context in comments.

3 Comments

Upvoted because this seems like a better approach than string manipulation to strip leading 0's, should also work in AS2 according to docos
Like how I answered this 2 months earlier but my answer was ignored! Nice!
@NickWiggill They key the distinguishes this answer from yours is Rahul has used the radix argument which yours was lacking. I also have no idea why I was so naive as to provide my existing answer but alas (I guess it was 3 years ago).
18
parseInt(yourString);

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

Comments

8

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:

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.

1 Comment

Thanks for this function, worked great! I assumed that's what this was doing, but this code saved me a lot of time heh
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.