Objective-C: format numbers to ordinals: 1, 2, 3, .. to 1st, 2nd, 3rd

后端 未结 5 1614
悲哀的现实
悲哀的现实 2021-01-04 00:57

In Objective C, is there any way to format an integer to ordinals 1 => \"1st\", 2 => \"2nd\" etc... that works for any language? So if the user

5条回答
  •  星月不相逢
    2021-01-04 01:41

    The solution is immediately available from NSNumberFormatter:

    - (NSString *)getOrdinalStringFromInteger:(NSInteger)integer
    {
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setLocale:[NSLocale currentLocale]];
        [formatter setNumberStyle:NSNumberFormatterOrdinalStyle];
        return [formatter stringFromNumber:[NSNumber numberWithInteger:integer]];
    }
    

提交回复
热议问题