Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return 1st from 1, 2nd from 2, ..., 11th from 11,

Is there a Objective-c function to return a NSString * from a NSUInteger, i.e.:

  • 1 -> 1st
  • 2 -> 2nd
  • ...
  • 10 -> 10th
  • 21 -> 21st
  • ...
  • 31 -> 31st

for the range from 1 to 31. Thanks!

like image 529
ohho Avatar asked Aug 20 '10 10:08

ohho


People also ask

WHAT IS 1st 2nd 3rd 4th called?

What are ordinal numbers from 1 to 10? The ordinal numbers from 1 to 10 are 1st – First, 2nd – Second, 3rd – Third, 4th – Fourth, 5th – Fifth, 6th – Sixth, 7th – Seventh, 8th – Eighth, 9th – Ninth and 10th – Tenth, respectively.


1 Answers

- (NSString *)stringFromInt:(int)num {
    NSString stringAddition;
    if (num%100 >= 11 && num%100 <= 13) {
        stringAddition = @"th";
    }
    else {
        switch (num % 10) {
            case 1: stringAddition = @"st"; break;
            case 2: stringAddition = @"nd"; break;
            case 3: stringAddition = @"rd"; break;
            default: stringAddition = @"th"; break;
        }
    }
    return [NSString stringWithFormat:@"%i%@", num, stringAddition];
}

EDIT:
Fixed the 11, 12, 13 issue.

like image 183
Michael Kessler Avatar answered Oct 04 '22 06:10

Michael Kessler