在c#中有一个简单的方法来创建一个数字的序数吗?例如:

1返回第1位 2返回第2 3返回第3 等

这是否可以通过String.Format()来完成,或者是否有可用的函数来完成?


当前回答

简单、干净、快捷

private static string GetOrdinalSuffix(int num)
{
    string number = num.ToString();
    if (number.EndsWith("11")) return "th";
    if (number.EndsWith("12")) return "th";
    if (number.EndsWith("13")) return "th";
    if (number.EndsWith("1")) return "st";
    if (number.EndsWith("2")) return "nd";
    if (number.EndsWith("3")) return "rd";
    return "th";
}

或者更好的是,作为一个扩展方法

public static class IntegerExtensions
{
    public static string DisplayWithSuffix(this int num)
    {
        string number = num.ToString();
        if (number.EndsWith("11")) return number + "th";
        if (number.EndsWith("12")) return number + "th";
        if (number.EndsWith("13")) return number + "th";
        if (number.EndsWith("1")) return number + "st";
        if (number.EndsWith("2")) return number + "nd";
        if (number.EndsWith("3")) return number + "rd";
        return number + "th";
    }
}

现在你可以打电话了

int a = 1;
a.DisplayWithSuffix(); 

甚至直接到

1.DisplayWithSuffix();

其他回答

FWIW,对于MS-SQL,这个表达式将完成工作。将第一个WHEN (WHEN num % 100 IN (11,12,13) THEN 'th')作为列表中的第一个,因为这依赖于在其他尝试之前尝试。

CASE
  WHEN num % 100 IN (11, 12, 13) THEN 'th' -- must be tried first
  WHEN num % 10 = 1 THEN 'st'
  WHEN num % 10 = 2 THEN 'nd'
  WHEN num % 10 = 3 THEN 'rd'
  ELSE 'th'
END AS Ordinal

对于Excel:

=MID("thstndrdth",MIN(9,2*RIGHT(A1)*(MOD(A1-11,100)>2)+1),2)

表达式(MOD(A1- 11100)>2)对于除以11,12,13结尾的任何数字(FALSE = 0)外的所有数字都是TRUE(1)。因此2 * RIGHT(A1) * (MOD(A1- 11100)>2) +1)对于11/12/13最终为1,否则: 1等于3 2点到5点, 3至7点 其他:9 -所需的2个字符从该位置开始的“第thstndrdth”中选择。

如果你真的想把它直接转换成SQL,这对我来说适用于一些测试值:

DECLARE @n as int
SET @n=13
SELECT SubString(  'thstndrdth'
                 , (SELECT MIN(value) FROM
                     (SELECT 9 as value UNION
                      SELECT 1+ (2* (ABS(@n) % 10)  *  CASE WHEN ((ABS(@n)+89) % 100)>2 THEN 1 ELSE 0 END)
                     ) AS Mins
                   )
                 , 2
                )

简单、干净、快捷

private static string GetOrdinalSuffix(int num)
{
    string number = num.ToString();
    if (number.EndsWith("11")) return "th";
    if (number.EndsWith("12")) return "th";
    if (number.EndsWith("13")) return "th";
    if (number.EndsWith("1")) return "st";
    if (number.EndsWith("2")) return "nd";
    if (number.EndsWith("3")) return "rd";
    return "th";
}

或者更好的是,作为一个扩展方法

public static class IntegerExtensions
{
    public static string DisplayWithSuffix(this int num)
    {
        string number = num.ToString();
        if (number.EndsWith("11")) return number + "th";
        if (number.EndsWith("12")) return number + "th";
        if (number.EndsWith("13")) return number + "th";
        if (number.EndsWith("1")) return number + "st";
        if (number.EndsWith("2")) return number + "nd";
        if (number.EndsWith("3")) return number + "rd";
        return number + "th";
    }
}

现在你可以打电话了

int a = 1;
a.DisplayWithSuffix(); 

甚至直接到

1.DisplayWithSuffix();

这是dart中的实现,可以根据语言进行修改。

String getOrdinalSuffix(int num){
    if (num.toString().endsWith("11")) return "th";
    if (num.toString().endsWith("12")) return "th";
    if (num.toString().endsWith("13")) return "th";
    if (num.toString().endsWith("1")) return "st";
    if (num.toString().endsWith("2")) return "nd";
    if (num.toString().endsWith("3")) return "rd";
    return "th";
}

虽然我还没有对此进行基准测试,但通过避免所有条件case语句,您应该能够获得更好的性能。

这是java,但是移植到c#很简单:

public class NumberUtil {
  final static String[] ORDINAL_SUFFIXES = {
    "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
  };

  public static String ordinalSuffix(int value) {
    int n = Math.abs(value);
    int lastTwoDigits = n % 100;
    int lastDigit = n % 10;
    int index = (lastTwoDigits >= 11 && lastTwoDigits <= 13) ? 0 : lastDigit;
    return ORDINAL_SUFFIXES[index];
  }

  public static String toOrdinal(int n) {
    return new StringBuffer().append(n).append(ordinalSuffix(n)).toString();
  }
}

注意,如果在一个紧密循环中生成大量序数,减少条件和使用数组查找应该会提高性能。然而,我也承认这并不像case语句解决方案那样可读。

public static string OrdinalSuffix(int ordinal)
{
    //Because negatives won't work with modular division as expected:
    var abs = Math.Abs(ordinal); 

    var lastdigit = abs % 10; 

    return 
        //Catch 60% of cases (to infinity) in the first conditional:
        lastdigit > 3 || lastdigit == 0 || (abs % 100) - lastdigit == 10 ? "th" 
            : lastdigit == 1 ? "st" 
            : lastdigit == 2 ? "nd" 
            : "rd";
}