Possible Duplicate:
NSNumberFormatter and ‘th’ ‘st’ ‘nd’ ‘rd’ (ordinal) number endings可能的重复:NSNumberFormatter和th ' st ' nd ' rd '(序号)数字结尾
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)
我正在构建一个应用程序,下载玩家的等级并显示它们。举个例子,你在所有玩家中排名第三,我插入了一个条件将它显示为第3,而不是第3我在第2和第1做了同样的事情。但是,当级别更高时,比如2883,它将显示2883
My question is, how can I get it to reformat the number to XXX1st, XXX2nd, XXX3rd etc?
我的问题是,如何将数字重新格式化为xxx1、xxx2、xxx3等等?
To show what I mean, here's how I format my number to add a "rd" if it's 3
为了显示我的意思,这里是我如何格式化我的数字来添加一个“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;
}
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?
我不能对2000年前的每一个数字都这样做(总共有2000个等级)——我能做什么来解决这个问题?
2 个解决方案
#1
1
Make it check the last digit of every number then add the suffix accordingly.
让它检查每个数字的最后一个数字,然后添加相应的后缀。
Checking the last 2 digits will fix it.
检查最后两位数就可以了。
#2
2
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
下面是另一种语言的一个简短片段:http://www.bytechaser.com/en/functions/b6yhfyxh78/convert- to-ordinal-like-1st-2nd-in- 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";
}
}
#1
1
Make it check the last digit of every number then add the suffix accordingly.
让它检查每个数字的最后一个数字,然后添加相应的后缀。
Checking the last 2 digits will fix it.
检查最后两位数就可以了。
#2
2
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
下面是另一种语言的一个简短片段:http://www.bytechaser.com/en/functions/b6yhfyxh78/convert- to-ordinal-like-1st-2nd-in- 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";
}
}