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

前端 未结 20 1219
粉色の甜心
粉色の甜心 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:17

    This will convert date to string and also add ordinal in the date. You can modify the date formatte by changing NSDateFormatter object

    -(NSString*) getOrdinalDateString:(NSDate*)date
    {
        NSString* string=@"";
        NSDateComponents *components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay fromDate:date];
    
        if(components.day == 1 || components.day == 21 || components.day == 31)
            string = @"st";
    
        else if (components.day == 2 || components.day == 22)
            string = @"nd";
    
        else if (components.day == 3 || components.day == 23)
            string = @"rd";
    
        else
            string = @"th";
    
    
        NSDateFormatter *dateFormatte = [[NSDateFormatter alloc] init];
        [dateFormatte setFormatterBehavior:NSDateFormatterBehavior10_4];
        [dateFormatte setDateFormat:[NSString stringWithFormat:@"d'%@' MMMM yyyy",string]];
    
        NSString *dateString = [dateFormatte stringFromDate:date];
        return dateString;
    }
    
    0 讨论(0)
  • 2020-12-03 01:18

    I'm not aware of this capability. However, it's possible to do this yourself. In English, the ordinal (th, st, nd, rd, etc) has a really simple pattern:

    If the number ends with: => Use:

    • 0 => th
    • 1 => st
    • 2 => nd
    • 3 => rd
    • 4 => th
    • 5 => th
    • 6 => th
    • 7 => th
    • 8 => th
    • 9 => th
    • 11 => th
    • 12 => th
    • 13 => th

    This will not spell out the word for you, but it will allow you to do something like: "42nd", "1,340,697th", etc.

    This gets more complicated if you need it localized.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-03 01:19

    Many of the solutions here don't handle higher numbers like 112. Here is a simple way to do it.

    for(int i=0;i<1000;i++){
        int n = i;
        NSString* ordinal = @"th";
        if(n%10==1 && n%100!=11) ordinal = @"st";
        if(n%10==2 && n%100!=12) ordinal = @"nd";
        if(n%10==3 && n%100!=13) ordinal = @"rd";
        NSLog(@"You are the %d%@",i,ordinal);
    }
    
    0 讨论(0)
  • 2020-12-03 01:22

    It's quite simple in English. Here's a swift extension:

    extension Int {
        var ordinal: String {
            get {
                var suffix = "th"
                switch self % 10 {
                    case 1:
                        suffix = "st"
                    case 2:
                        suffix = "nd"
                    case 3:
                        suffix = "rd"
                    default: ()
                }
                if 10 < (self % 100) && (self % 100) < 20 {
                    suffix = "th"
                }
                return String(self) + suffix
            }
        }
    }
    

    Then call something like:

        cell.label_position.text = (path.row + 1).ordinal
    
    0 讨论(0)
  • 2020-12-03 01:22

    Just adding another implementation as a class method. I didn't see this question posted until after I implemented this from an example in php.

    + (NSString *)buildRankString:(NSNumber *)rank
    {
        NSString *suffix = nil;
        int rankInt = [rank intValue];
        int ones = rankInt % 10;
        int tens = floor(rankInt / 10);
        tens = tens % 10;
        if (tens == 1) {
            suffix = @"th";
        } else {
            switch (ones) {
                case 1 : suffix = @"st"; break;
                case 2 : suffix = @"nd"; break;
                case 3 : suffix = @"rd"; break;
                default : suffix = @"th";
            }
        }
        NSString *rankString = [NSString stringWithFormat:@"%@%@", rank, suffix];
        return rankString;
    }
    
    0 讨论(0)
提交回复
热议问题