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.

greg
  • 333
  • 2
  • 3
  • 9

4 Answers4

30

In AS3, you can try:

parseInt('04755', 10)

10 above is the radix.

António Almeida
  • 9,620
  • 8
  • 59
  • 66
Rahul Singhai
  • 1,299
  • 15
  • 27
  • Upvoted because this seems like a better approach than string manipulation to strip leading 0's, should also work in AS2 according to docos – Mike Demenok Jan 22 '14 at 08:19
  • 3
    Like how I answered this 2 months earlier but my answer was ignored! Nice! – Engineer Mar 25 '14 at 08:43
  • 2
    @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). – Marty Jun 17 '14 at 22:55
18
parseInt(yourString);

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

Marty
  • 39,033
  • 19
  • 93
  • 162
Engineer
  • 8,529
  • 7
  • 65
  • 105
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.

Marty
  • 39,033
  • 19
  • 93
  • 162
  • Thanks for this function, worked great! I assumed that's what this was doing, but this code saved me a lot of time heh – vanhornRF May 27 '11 at 18:00
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");
Jakir Hossen
  • 451
  • 4
  • 13