AS3: How to Convert Number to String

Contributor Icon Contributed by David Kirk     
Tag Icon Tagged: 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);

 

17 Comments -


  1. littleman80 said on November 11, 2008

    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.)?…

  2. justin said on January 7, 2009

    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

  3. jk said on February 20, 2009

    How to do whith that?:

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

  4. Daniel said on April 28, 2009

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

  5. jenny said on May 22, 2009

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

  6. Roberto said on May 25, 2009

    @ 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

  7. Christo said on June 2, 2009

    That doesn’t work.

  8. abe said on January 21, 2010

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

  9. Wu Di said on April 21, 2010

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

  10. Shpagatus said on July 5, 2010

    Thanks

  11. Kol Sangvan said on July 17, 2010

    this site is better research

  12. SantaMan said on December 1, 2010

    Nice and simple….

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

    No functions. No Mess :-)

  13. Mikale Erhart said on April 2, 2011

    Thanks! Helped me right out with what I needed.

  14. fussBudget said on June 25, 2011

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

  15. Starry Guy said on August 7, 2011

    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

  16. Strony flash said on August 21, 2011

    Still the same problem, my code is:

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

  17. ape23 said on August 30, 2011

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

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -