A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: converting a string to number array

  1. #1
    Junior Member
    Join Date
    Aug 2006
    Posts
    12

    converting a string to number array

    basicaly if you can read the code then you understand what i am doing sorry i cant realy explain what it is doing but if you can read action script then you can under stand what i am doing
    if you run this script it output this

    1
    0
    1000
    100
    10
    final =undefined10100010010
    everything is working unil the code reaches to finaly
    what i am trying to do is add them all like a math equation
    but what the script is doing is joining the script like a string
    does any body know how i can convert this string into a number
    so the script will at final add and not join together like a string
    not php actionscript
    PHP Code:
    var my_str:String = new String("10444");
    var 
    my_array:Array = my_str.split("");
    var 
    length:Number=my_array.length;
    var 
    whilenum:Number 1;
    for (var 
    0i<my_array.lengthi++) {
        if(
    my_array[i]== 4)
        {
            var 
    digit:Number;
            var 
    full:Number =10;
            
            
    digit my_array.length i;
            
    digit digit -1;
            for(var 
    =0digitb++)
            {
            
    full =10 full
            }
                
            
    my_array[i] = full;
              
    full 10;
              
            }

        
    trace(my_array[i]);

    }
    var final:
    Number;
    for( var 
    0my_array.lengthc++)
    {
        final = final + 
    my_array[c]; 
        }
        
    trace("final ="+ final); 
    i am sorry i cant realy explain what i am doing but if you have a question or need information i would be happily to answer it as the best i can
    thank you in advance

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    var final:Number = 0;
    for( var c = 0; c < my_array.length; c++)
    {
    final = final + Number(my_array[c]);
    }
    trace("final ="+ final);



    you can also write the addding in shorthand like:

    final += Number(my_array[c]);
    Last edited by silentweed; 01-14-2007 at 03:36 PM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center