Script.it .Nam Myoho Renghe Kyo.

Script.it
[AS3.0 / JS / XCode / Java] Decimal formatting numbers

So you have a number and want to apply some decimal separators to it.
For example:
1000 -> 1.000
10000 -> 10.000

Here are the AS3.0, Javascript, Objective-C and Java codes to do so.
If you look closely there is not really a big difference between languages.
For OO languages almost all structures are the same as any other OO language.
So if you want to be a better coder. Switch between languages and try and see the similarities,
this will help you get more understanding and insight in a global view of all OO languages.
For now… here are some codes for decimal formatting. I intentially left out doing it with RegExp.

Actionscript3.0 version:

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
public  function formatWithDecimalSeperators( value:String ):String {
     var rev    :String = "";
     var result:String = "";
     var count :int    = 0;
     for( var i:int = value.length-1; i >= 0; i -- ){
          rev = rev + value.substr(i, 1);
          if( ++count%3==0 && count <= value.length -1)  rev = rev + ".";
     }
     for( i = rev.length-1; i >= 0; i -- ) result = result + rev.substr(i, 1);
     return result;
}



Javascript version:

?View Code JAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
function formatWithDecimalSeperators( value ){
     rev    = "";
     result = "";
     count  = 0;
     for( i = value.length-1; i >= 0; i--){
          rev = rev + value.substring( i, i+1);
          if(++count%3 == 0 && count <= value.length-1) rev = rev + ".";
     }
     for( i = rev.length-1; i>=0;i--) result = result + rev.substring( i, i+1);
     return result;
}



Objective-C version:

?View Code OBJECTIVE-C
1
2
3
4
5
6
7
8
9
10
11
+(NSString *) formatWithDecimalSeperators: (NSString *)subject {
    NSString *rev    = @"";
    NSString *result = @"";
    int count = 0;
    for( int i = [subject length]-1; i >= 0; i--){
        rev  = [NSString stringWithFormat:@"%@%@", rev, [subject substringWithRange:NSMakeRange(i, 1)]];
        if( ++count%3==0 && count <= [subject length]-1) rev = [NSString stringWithFormat:@"%@%@", rev, @"."];
    }
    for( int i = [rev length]-1; i>=0; i--)result = [NSString stringWithFormat:@"%@%@", result, [rev substringWithRange:NSMakeRange(i, 1)]];
    return result;
}



Java version:

1
2
3
4
5
6
7
8
9
10
11
public String formatWithDecimalSeperators( String value ){
     String rev = "";
     String result = "";
     int count = 0;
     for( int i = value.length()-1; i>=0;i--){
          rev = rev + value.substring(i, i+1);
          if(++count%3==0&&count<=value.length()-1) rev = rev + ".";
     }
     for( int i = rev.length()-1; i>=0; i--) result = result + rev.substring(i, i+1);
     return result;
}



Happy coding!
\o
Rackdoll

Creative Commons License
[AS3.0 / JS / XCode / Java] Decimal formatting numbers by Script.it, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Netherlands License.

Comments are closed.

Categories
Archives
Live Supporters