可能的重复:
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?
发布评论
评论(2)
这是另一种语言的简短片段: http://www.bytechaser.com/en/functions/b6yhfyxh78/convert-number-to-ordinal-like-1st-2nd-in-c-sharp.aspx
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
让它检查每个数字的最后一位数字,然后相应地添加后缀。
检查最后 2 位数字即可解决此问题。
Make it check the last digit of every number then add the suffix accordingly.
Checking the last 2 digits will fix it.