AS3 Quick Tips: String to Number Conversion (and vice versa)

If this article helped, please drop a comment at the bottom.

Quick Code:

// string to number
var myString:String = "5";
var myNumber:Number = Number(myString);
// number to string
var myNumber:Number= 5;
var myString:String= String(myNumber);
// string to int (integer)
var myString:String = "5";
var myInt:int = int(myString);

Hello and welcome to the start of a new line of AS3 articles, focusing on being very short and to the point. so anyway i was walking down the road and i caught the glimps of a penny on the floor, so i went to pick it up and accidently fell into an alternate dimention where i met a rather large yellow creature proclaiming to be the king of AS3. So i just said stage.removeChild(Monster_mc); and he was defeated…

…then i wrote this tutorial.

OK so, why would you want to convert a String to a Number and vice versa? Well it could be for a whole range of reasons though mainly if flash spits out this message:

“1067: Implicit coercion of a value of type String to an unrelated type Number.”

You mostly get this if you’ve tried to run a numerical calculation on a string, which just dosen’t work. However hope is not lost! you can use two of AS3′s handy functions to convert them on the fly!
Say i have these two variables, one a String, the other a Number.

var myString:String = "123";
var myNumber:Number = 42;

Now say you want to add them together.

var myCalc:Number = myNumber + myString;
trace(myCalc);

If you ran that calculation you’d get the error mentioned above. However if you convert the ‘myString’ varible to a Number the calculation will run perfectly.

var myCalc:Number = myNumber + Number(myString);
trace(myCalc);

this will now output 165 in the output window.

if you do the same but convert the Number to a string like so…

(remember to change the var ‘myCalc’ to a String)

var myCalc:String = String(myNumber) + myString;
trace(myCalc);

Flash will output 42123 as it would if you were adding to a string.

Well thats it, if you think i’ve missed something please leave a comment :)

Tags: , , , , , , , ,

  • http://knowace.com Thomas Garrod

    This is just what I needed.Thanks! Well clear and neatly done.

  • viaria

    i wonder about, how can i use a string for a mivieclip instance name,

  • Anonymous

    thanks! easier than i thought it would be

  • waunan

    Thanks a ton…you have tipped the love/hate relationship with as3 in a favorable direction.
    Very concise, understandable, and best of all,
    it actually works…have a good day

  • http://www.healthrx.com Patrick

    Many thanks! Needed a quick conversion tip.

  • Tim

    Tnx! :)

  • George

    Thanks!

  • Ryan

    nice sooo simple, but saved me, ty!

  • Josh

    Thanks dude!

  • Andrews

    THank you very much, I was looking for exqactly this bit of code… very precise & short, and very well defined..

  • http://www.cagriozdes.com Cagri

    Hey, thank you for the tut : )
    I havent run the code yet, but when I run it and it fails, I ll be back with my hater spam :P kidding : )
    very simple, very clean, thanks a lot!

  • http://www.cagriozdes.com Cagri

    Worked! never thought it would be that easy! YAY!

  • elham

    nice
    tnx

  • http://www.agivera.pl agi

    thx :)

  • incognito

    wtf!!

  • Anonymous

    helped, thanks

  • Ken

    What if you want to display this within a dynamic textfield? I still get “1067: Implicit coercion of a value of type String to an unrelated type Number.”

  • Italo

    Great, that was what I needed!

  • http://3dreaction.blogspot.com Iqtidar Ali

    Ken,
    Store the result in a new variable and than display it in the dynamic text field,

    // string to int (integer)
    var myString:String = “5″;
    var myInt:int = int(myString);
    var myResult = myInt;

    myTextField.text = myResult;

    Kool.
    http://www.souldesigns.net

  • http://myboodesign.com Boo

    great!!!! thank mate..!!!

  • Krazay

    Excellent… made my day way easier :)

  • Anonymous

    thank you. it works.

  • Behnam

    It worked for me. Thanks

  • Rogerio Gauchinho

    Tchê! Sou Web Designer e ficoi muito grato por tua explicação.

    Valeu mesmo!!!

    Paulo Fonseca

  • Jerodnelson

    Well lol, you asked this over 2 years ago buuuut, someone else may see this and it may help them. The way objects in as3 are nested within the display list is in array form, so therefore you can use a string to call the item in the array that shares the name of the string.
    e.g.   if your movieclip is on the same timeline you are writing the code you can say : this["movieClipName"]  and you would get your movieclip titled movieClipName.  If you had another one inside that for instance you would then go :
    this["movieClipName"]["anotherClip"]      (written like a multi-dimensional array) you are able to access all the children of the displaylist this way. 
    Hope this helped someone. Also great little elegant solution from OP. thx.

  • Joshuapangan

    Great! it works like a charm!

  • Frank

    very helpful thank you

  • Mohammed1022

    thanks

  • Mgcool

    Everything ok, faster and easier to find than in Adobe’s help. Regards. México.

  • tugBUT

    Thanks much!

  • Fold

    thanks!

  • Richard Rosenthal

    If you aren’t sure about the source of your string, ie, parsing input from an xml file or http request, I would recommend sanitizing it in case it was not properly in the form of a number. For example

    var theNumber:Number = Number(theStringVar);
    if( ! theNumber ) theNumber=0;

    Number() will NOT always return a number – it can also return NaN, which you cannot use for math. by doing if( ! theNumber ) theNumber=0; you can default the value to zero if it was an incompatible string.