NSNumberFormatter and 'th' 'st' 'nd' 'rd' (ordinal) number endings

前端 未结 20 1308
粉色の甜心
粉色の甜心 2020-12-03 00:38

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.

20条回答
  •  没有蜡笔的小新
    2020-12-03 01:19

    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;
    }
    

提交回复
热议问题