NSNumberFormatter and 'th' 'st' 'nd' 'rd' (ordinal) number endings
Posted
by
jan
on Stack Overflow
See other posts from Stack Overflow
or by jan
Published on 2010-07-22T20:04:52Z
Indexed on
2010/12/23
19:54 UTC
Read the original article
Hit count: 336
objective-c
|nsnumberformatter
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.
+(NSString*)ordinalNumberFormat:(NSInteger)num{
NSString *ending;
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];
}
Adapted from nickf's answer here http://stackoverflow.com/questions/69262/is-there-an-easy-way-in-net-to-get-st-nd-rd-and-th-endings-for-numbers
© Stack Overflow or respective owner