Need help to make this method prettier
Need help to make this method prettier
- Subject: Need help to make this method prettier
- From: Andre Masse <email@hidden>
- Date: Sat, 01 Nov 2008 13:56:42 -0400
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 (email@hidden)
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:
This email sent to email@hidden