Created
July 16, 2012 00:40
-
-
Save rgerard/3119444 to your computer and use it in GitHub Desktop.
NSString category that returns an NSNumber with the correct ordinal ending. This was borrowed from http://stackoverflow.com/questions/3312935/nsnumberformatter-and-th-st-nd-rd-ordinal-number-endings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSString (Additions) | |
+ (NSString*)ordinalNumberFormat:(NSNumber *)numObj; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSString+Additions.h" | |
@implementation NSString (Additions) | |
+ (NSString*)ordinalNumberFormat:(NSNumber *)numObj { | |
NSString *ending; | |
NSInteger num = [numObj integerValue]; | |
int ones = num % 10; | |
int tens = floor(num / 10); | |
tens = tens % 10; | |
if(tens == 1){ | |
ending = @"th"; | |
} else { | |
switch (ones) { | |
case 1: | |
ending = @"st"; | |
break; | |
case 2: | |
ending = @"nd"; | |
break; | |
case 3: | |
ending = @"rd"; | |
break; | |
default: | |
ending = @"th"; | |
break; | |
} | |
} | |
return [NSString stringWithFormat:@"%d%@", num, ending]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment