# Better way to format String!?

**URL:** <https://forum.kirupa.com/t/better-way-to-format-string/324470>\
**Category:** flash\
**Created:** [August 22, 2011, 8:04pm UTC](https://forum.kirupa.com/t/better-way-to-format-string/324470 "2011-08-22T20:04:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thovas](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@Thovas](https://forum.kirupa.com/u/Thovas)\
**Post date:** [August 22, 2011, 8:04pm UTC](https://forum.kirupa.com/t/better-way-to-format-string/324470/1 "2011-08-22T20:04:35Z")

</div>

Hello,

I just finished an application that do some calculations and the end result is displayed on the stage in TextFields. The result string was in need of formating from example: 123456789.01 to 123 456 789.01 so i kinda made a function that format it.

```php
function formatNum(num:Number):String{    
 var count:int;   
 var numString:String = String(num);

 //counting the character before the decimal point
     for (var i:int = 0; i < numString.length; i++){    
        if(numString.charAt(i) == "."){  
            trace("count: " + count); 
            break;       
        }else{    
            count ++;      
        }   
    } 
 
    //calculating the amount of pauses(a.k.a " " )   
   var pauseNum:int = count/3;       

   //inserting the pauses and updating the String

    for(var p:int =0; p<pauseNum; p++){ 
       numString = numString.substr(0, count -((p+1)*3)) + " " + numString.substr(count -((p+1)*3),numString.length);  
    }    
        //returning formated String.
       // the value is cut 2 characters after the decimal point

       return numString.substr(0,count + pauseNum +3);
}

```

all this code is used simply :

```php
var val:Number = 123456789.0123
displayTxt.text = formatNum(val);

```

My question is “Is there a better way to do this formating?”

Thanks in advance  
Regards Thovas
