Search code examples
iphonecocoaintegerordinals

Integer Extensions - 1st, 2nd, 3rd etc


Possible Duplicate:
NSNumberFormatter and ‘th’ ‘st’ ‘nd’ ‘rd’ (ordinal) number endings

Hello,

I'm building an application that downloads player ranks and displays them. So say for example, you're 3rd out of all the players, I inserted a condition that will display it as 3rd, not 3th and i did the same for 2nd and 1st. When getting to higher ranks though, such as 2883rd, it'll display 2883th (for obvious reasons)

My question is, how can I get it to reformat the number to XXX1st, XXX2nd, XXX3rd etc?

To show what I mean, here's how I format my number to add a "rd" if it's 3

if ([[container stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@"3"])
{
    NSString*badge = [NSString stringWithFormat:@"%@rd",[container stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    NSString*scoreText = [NSString stringWithFormat:@"ROC Server Rank: %@rd",[container stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    profile.badgeValue = badge;
    rank.text = scoreText;
}

I can't do this for every number up to 2000 (there are 2000 ranks in total) - what can I do to solve this problem?


Solution

  • Make it check the last digit of every number then add the suffix accordingly.

    Checking the last 2 digits will fix it.