Is there a way to use NSNumberFormatter to get the \'th\' \'st\' \'nd\' \'rd\' number endings?
EDIT:
Looks like it does not exist. Here\'s what I\'m using.
This will convert date to string and also add ordinal in the date. You can modify the date formatte by changing NSDateFormatter object
-(NSString*) getOrdinalDateString:(NSDate*)date
{
NSString* string=@"";
NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay fromDate:date];
if(components.day == 1 || components.day == 21 || components.day == 31)
string = @"st";
else if (components.day == 2 || components.day == 22)
string = @"nd";
else if (components.day == 3 || components.day == 23)
string = @"rd";
else
string = @"th";
NSDateFormatter *dateFormatte = [[NSDateFormatter alloc] init];
[dateFormatte setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatte setDateFormat:[NSString stringWithFormat:@"d'%@' MMMM yyyy",string]];
NSString *dateString = [dateFormatte stringFromDate:date];
return dateString;
}
I'm not aware of this capability. However, it's possible to do this yourself. In English, the ordinal (th, st, nd, rd, etc) has a really simple pattern:
If the number ends with: => Use:
This will not spell out the word for you, but it will allow you to do something like: "42nd", "1,340,697th", etc.
This gets more complicated if you need it localized.
This does the trick in one method (for English). Thanks nickf https://stackoverflow.com/a/69284/1208690 for original code in PHP, I just adapted it to objective C
:-
-(NSString *) addSuffixToNumber:(int) number
{
NSString *suffix;
int ones = number % 10;
int tens = (number/10) % 10;
if (tens ==1) {
suffix = @"th";
} else if (ones ==1){
suffix = @"st";
} else if (ones ==2){
suffix = @"nd";
} else if (ones ==3){
suffix = @"rd";
} else {
suffix = @"th";
}
NSString * completeAsString = [NSString stringWithFormat:@"%d%@", number, suffix];
return completeAsString;
}
Many of the solutions here don't handle higher numbers like 112. Here is a simple way to do it.
for(int i=0;i<1000;i++){
int n = i;
NSString* ordinal = @"th";
if(n%10==1 && n%100!=11) ordinal = @"st";
if(n%10==2 && n%100!=12) ordinal = @"nd";
if(n%10==3 && n%100!=13) ordinal = @"rd";
NSLog(@"You are the %d%@",i,ordinal);
}
It's quite simple in English. Here's a swift extension:
extension Int {
var ordinal: String {
get {
var suffix = "th"
switch self % 10 {
case 1:
suffix = "st"
case 2:
suffix = "nd"
case 3:
suffix = "rd"
default: ()
}
if 10 < (self % 100) && (self % 100) < 20 {
suffix = "th"
}
return String(self) + suffix
}
}
}
Then call something like:
cell.label_position.text = (path.row + 1).ordinal
Just adding another implementation as a class method. I didn't see this question posted until after I implemented this from an example in php.
+ (NSString *)buildRankString:(NSNumber *)rank
{
NSString *suffix = nil;
int rankInt = [rank intValue];
int ones = rankInt % 10;
int tens = floor(rankInt / 10);
tens = tens % 10;
if (tens == 1) {
suffix = @"th";
} else {
switch (ones) {
case 1 : suffix = @"st"; break;
case 2 : suffix = @"nd"; break;
case 3 : suffix = @"rd"; break;
default : suffix = @"th";
}
}
NSString *rankString = [NSString stringWithFormat:@"%@%@", rank, suffix];
return rankString;
}