整数扩展 - 第一、第二、第三等

发布于 2024-09-11 07:38:56 字数 1077 浏览 8 评论 0 原文

可能的重复:
NSNumberFormatter 和 'th' 'st' 'nd' 'rd' (序数)数字结尾

您好,

我正在构建一个应用程序,用于下载玩家排名并显示它们。举例来说,您是所有玩家中的第三名,我插入了一个条件,将其显示为第三名,而不是第三名,我对第二名和第一名做了同样的事情。当达到更高的排名时,例如第 2883 位,它会显示第 2883 位(出于明显的原因)

我的问题是,如何才能将数字重新格式化为 XXX1st、XXX2nd、XXX3rd 等?

为了说明我的意思,以下是我如何格式化我的数字以添加“rd”(如果它是 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 个排名) - 我该怎么做才能解决这个问题?

Possible Duplicate:
NSNumberFormatter and ‘th’ ‘st’ ‘nd’ ‘rd’ (ordinal) number endings

Hello,

I'm building an application that downloads player ranks and displays them. So say for example, you're 3rd out of all the players, I inserted a condition that will display it as 3rd, not 3th and i did the same for 2nd and 1st. When getting to higher ranks though, such as 2883rd, it'll display 2883th (for obvious reasons)

My question is, how can I get it to reformat the number to XXX1st, XXX2nd, XXX3rd etc?

To show what I mean, here's how I format my number to add a "rd" if it's 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;
}

I can't do this for every number up to 2000 (there are 2000 ranks in total) - what can I do to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知你几分 2024-09-18 07:38:56

这是另一种语言的简短片段: 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";
    }
}

Here's a short snippet in another language: 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";
    }
}
面如桃花 2024-09-18 07:38:56

让它检查每个数字的最后一位数字,然后相应地添加后缀。

检查最后 2 位数字即可解决此问题。

Make it check the last digit of every number then add the suffix accordingly.

Checking the last 2 digits will fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文