Hi,

I'm implementing a custom NSFormatter. I want a number with 9 digits displayed as "123 456 789". So before implementing the formatter I made a test project to check the conversion. The version I did get the job done but its not pretty :-) Any more good looking solutions?

Thanks,

Andre Masse



Here the meat:


- formatting a number from 123456789 to 123 456 789

- (IBAction)compute:(id)sender
{
        NSNumber *number = [NSNumber numberWithInt:[aValue intValue]];
        NSString *str0;
        NSString *str1;
        NSString *str2;
        
        int num = [number intValue]; //123456789

        int first = num / 1000000; // ->123
        int second = (num - first * 1000000) / 1000; //-> 456
        int third = (num - (first * 1000000) - (second * 1000)); // -> 789
        
        if (first < 10) {
                str0 = [NSString stringWithFormat:@"00%d", first];
        }
        else if (first < 100){
                str0 = [NSString stringWithFormat:@"0%d", first];
        }
        else {
                str0 = [NSString stringWithFormat:@"%d", first];
        }
        
        if (second < 10) {
                str1 = [NSString stringWithFormat:@"00%d", second];
        }
        else if (second < 100){
                str1 = [NSString stringWithFormat:@"0%d", second];
        }
        else {
                str1 = [NSString stringWithFormat:@"%d", second];
        }
        
        if (third < 10) {
                str2 = [NSString stringWithFormat:@"00%d", third];
        }
        else if (third < 100){
                str2 = [NSString stringWithFormat:@"0%d", third];
        }
        else {
                str2 = [NSString stringWithFormat:@"%d", third];
        }
NSString *retValue = [NSString stringWithFormat:@"%@ %@ %@", str0, str1, str2];
        [aResult setStringValue:retValue];
        
}
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to