Integer extensions - 1, 2, 3, etc.

Possible duplicate:
NSNumberFormatter and & lsquo; th & rsquo; & Lsquo; st & rsquo; & Lsquo; th & rsquo; & Lsquo; th & rsquo; (serial number) endings

Hello,

I am creating an application that downloads rows of players and displays them. So say, for example, that you are the third of all players, I inserted a condition that displays it as 3rd, not 3rd, and I did the same for 2nd and 1st. When you fall into higher ranks, such as the 2883rd, it displays the 2883th (for obvious reasons)

My question is how can I reformat it to XXX1st, XXX2nd, XXX3rd, etc.

To show what I mean, here is how I format my number to add β€œrd” if it is 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;
}

2000 ( 2000 ) - ?

+3
2

, , .

.

+1

: http://www.bytechaser.com/en/functions/b6yhfyxh78/convert-number-to-ordinal-like-1st-2nd-in-c-sharp.aspx

/// <summary>
/// Create an ordinal number from any number 
/// e.g. 1 becomes 1st and 22 becomes 22nd
/// </summary>
/// <param name="number">Number to convert</param>
/// <returns>Ordinal value as string</returns>
public static string FormatOrdinalNumber(int number)
{
    //0 remains just 0
    if (number == 0) return "0";
    //test for number between 3 and 21 as they follow a 
    //slightly different rule and all end with th            
    if (number > 3 && number < 21)
    {
        return number + "th";
    }
    //return the last digit of the number e.g. 102 is 2
    var lastdigit = number % 10;
    //append the correct ordinal val
    switch (lastdigit)
    {
        case 1: return number + "st";
        case 2: return number + "nd";
        case 3: return number + "rd";
        default: return number + "th";
    }
}
+2

Source: https://habr.com/ru/post/1756229/


All Articles