AS3: How to Convert Number to String

   Posted May 6, 2007 by David Kirk in Computer programming

Frequently, it is nice to convert numbers to strings in actionscript 3. For example, this will correct the error — 1067: Implicit coercion of a value of type Number to an unrelated type String.


In my neverending journey to teach myself actionscript, I have once again started pounding through some code.

In short to convert a number to a string, use the following construct:

text = String(number);

If you try to avoid this step, it’ll throw the following error…

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

Here is some example code that will throw the error:


var time:Date= new Date();
var txt:String;
txt = time.milliseconds;

This code works correctly:


var time:Date= new Date();
var txt:String;
txt = String(time.milliseconds);

 

About David Kirk

David Kirk is one of the original founders of tech-recipes and is currently serving as editor-in-chief. Not only has he been crafting tutorials for over ten years, but in his other life he also enjoys taking care of critically ill patients as an ICU physician.
View more articles by David Kirk

The Conversation

Follow the reactions below and share your own thoughts.

  • littleman80

    how about if I have a number, i.e.: 23 and I want to converted it to the string “0023” (with 4 digits i.e.)?…

    • fussBudget

      i.e. does NOT mean ‘for example’.  Be a big man and learn e.g.

  • justin

    same problem
    I’m getting a number from an XML, looks like 123,456,78
    i then myString.split(“,”).join(“”);
    gives me “12345678”
    now I want to use it in some math,
    i tried Number(myString);
    that just gives me the error above

  • jk

    How to do whith that?:

    var D:String;
    D = String(b * b – 4 * a * c);

  • Daniel

    thats cool man, but how would you know wich class to add so you can use the “string()” converter

  • jenny

    or you could use (time.milliseconds).toString()

    • Christo

      That doesn’t work.

  • @ littleman80
    var num:Number = 23;
    var StringConverted:String = “00”+String(num);
    trace(StringConverted);

    @justin
    to convert a string to number use this
    var TheString:String = “12345678”;
    var TheNumber:Number = parseInt(TheString);
    trace(TheNumber * 2) // by using * u can test that its really a number

  • abe

    I need a quick solution – without looking at my notes and when I googled and this page came first. thank you.

  • Thanks for the tip, but how to preserver decimal places?

  • Shpagatus

    Thanks

  • Kol Sangvan

    this site is better research

  • SantaMan

    Nice and simple….

    var myNumber:Number = 33;
    var myString = myNumber + “”;

    No functions. No Mess 🙂

  • Mikale Erhart

    Thanks! Helped me right out with what I needed.

  • No help at all. I converted the amount of the instore currency to a string and all I got was deied. Heres the code:
    credits.text=MochiCoins.inventory.storecredits+””
    credits is the dynamic text box
    MochiCoins.inventory.storecredits is the path to the store credits via mochi media

  • Still the same problem, my code is:

    var five:Number = 5;
    var six:Number = 6;
    var sum:Number = Number(five)+ Number(six);
    trace(suma);

  • shouldnt it say:
    var sum:Number = five + six;
    ?