Skip to content

Instantly share code, notes, and snippets.

@tomgco
Created June 12, 2011 12:05
Show Gist options
  • Save tomgco/1021489 to your computer and use it in GitHub Desktop.
Save tomgco/1021489 to your computer and use it in GitHub Desktop.
Generate a ordinal suffix for a given int in Objective-C
- (NSString*)getOrdinalSuffix: (int)number {
NSArray *suffixLookup = [NSArray arrayWithObjects:@"th",@"st",@"nd",@"rd",@"th",@"th",@"th",@"th",@"th",@"th", nil];
if (number % 100 >= 11 && number % 100 <= 13) {
return [NSString stringWithFormat:@"%d%@", number, @"th"];
}
return [NSString stringWithFormat:@"%d%@", number, [suffixLookup objectAtIndex:(number % 10)]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment