关于格式:.NET是否有一种简单的方法来获取数字的“ st”,“ nd”,“ rd”和“ th”结尾?

关于格式:.NET是否有一种简单的方法来获取数字的“ st”,“ nd”,“ rd”和“ th”结尾?

Is there an easy way in .NET to get “st”, “nd”, “rd” and “th” endings for numbers?

本问题已经有最佳答案,请猛点这里访问。

我想知道.NET中是否缺少转换以下内容的方法或格式字符串:

1
2
3
4
5
6
7
   1 to 1st
   2 to 2nd
   3 to 3rd
   4 to 4th
  11 to 11th
 101 to 101st
 111 to 111th

该链接提供了编写自己的函数所涉及的基本原理的错误示例,但我更好奇是否缺少我所需要的内置功能。

Scott Hanselman的答案是被接受的,因为它直接回答了问题。

对于解决方案,请参见此好答案。


这个功能比您想象的要简单得多。尽管为此可能已经存在一个.NET函数,但是以下函数(用PHP编写)可以完成此工作。移植起来应该不难。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function ordinal($num) {
    $ones = $num % 10;
    $tens = floor($num / 10) % 10;
    if ($tens == 1) {
        $suff ="th";
    } else {
        switch ($ones) {
            case 1 : $suff ="st"; break;
            case 2 : $suff ="nd"; break;
            case 3 : $suff ="rd"; break;
            default : $suff ="th";
        }
    }
    return $num . $suff;
}

简单,干净,快捷

1
2
3
4
5
6
7
8
9
10
11
    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";
    }

或更好,作为一种扩展方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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";
    }
}

现在你可以打电话

1
2
int a = 1;
a.DisplayWithSuffix();

甚至直接

1
1.DisplayWithSuffix();

不,.NET基本类库中没有内置功能。


@nickf:这是C#中的PHP函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static string Ordinal(int number)
{
    string suffix = String.Empty;

    int ones = number % 10;
    int tens = (int)Math.Floor(number / 10M) % 10;

    if (tens == 1)
    {
        suffix ="th";
    }
    else
    {
        switch (ones)
        {
            case 1:
                suffix ="st";
                break;

            case 2:
                suffix ="nd";
                break;

            case 3:
                suffix ="rd";
                break;

            default:
                suffix ="th";
                break;
        }
    }
    return String.Format("{0}{1}", number, suffix);
}

已经讨论过了,但是我不确定如何链接到它。这是代码段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    public static string Ordinal(this int number)
    {
        var ones = number % 10;
        var tens = Math.Floor (number / 10f) % 10;
        if (tens == 1)
        {
            return number +"th";
        }

        switch (ones)
        {
            case 1: return number +"st";
            case 2: return number +"nd";
            case 3: return number +"rd";
            default: return number +"th";
        }
    }

仅供参考:这是一种扩展方法。如果您的.NET版本低于3.5,则删除此关键字

[编辑]:感谢您指出这是不正确的,这就是复制/粘贴代码得到的结果:)


这是Microsoft SQL Server Function版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
CREATE FUNCTION [Internal].[GetNumberAsOrdinalString]
(
    @num int
)
RETURNS nvarchar(max)
AS
BEGIN

    DECLARE @Suffix nvarchar(2);
    DECLARE @Ones int;  
    DECLARE @Tens int;

    SET @Ones = @num % 10;
    SET @Tens = FLOOR(@num / 10) % 10;

    IF @Tens = 1
    BEGIN
        SET @Suffix = 'th';
    END
    ELSE
    BEGIN

    SET @Suffix =
        CASE @Ones
            WHEN 1 THEN 'st'
            WHEN 2 THEN 'nd'
            WHEN 3 THEN 'rd'
            ELSE 'th'
        END
    END

    RETURN CONVERT(nvarchar(max), @num) + @Suffix;
END

我知道这不是OP问题的答案,但是因为我发现从该线程中提升SQL Server函数很有用,所以这是等效的Delphi(Pascal):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function OrdinalNumberSuffix(const ANumber: integer): string;
begin
  Result := IntToStr(ANumber);
  if(((Abs(ANumber) div 10) mod 10) = 1) then // Tens = 1
    Result := Result + 'th'
  else
    case(Abs(ANumber) mod 10) of
      1: Result := Result + 'st';
      2: Result := Result + 'nd';
      3: Result := Result + 'rd';
      else
        Result := Result + 'th';
    end;
end;

...,-1st,0th有意义吗?


另一种味道:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// <summary>
/// Extension methods for numbers
/// </summary>
public static class NumericExtensions
{
    /// <summary>
    /// Adds the ordinal indicator to an integer
    /// </summary>
    /// <param name="number">The number</param>
    /// <returns>The formatted number</returns>
    public static string ToOrdinalString(this int number)
    {
        // Numbers in the teens always end with"th"

        if((number % 100 > 10 && number % 100 < 20))
            return number +"th";
        else
        {
            // Check remainder

            switch(number % 10)
            {
                case 1:
                    return number +"st";

                case 2:
                    return number +"nd";

                case 3:
                    return number +"rd";

                default:
                    return number +"th";
            }
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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";
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
else if (choice=='q')
{
    qtr++;

    switch (qtr)
    {
        case(2): strcpy(qtrs,"nd");break;
        case(3):
        {
           strcpy(qtrs,"rd");
           cout<<"End of First Half!!!";
           cout<<" hteam"<<"["<<hteam<<"]"<<hs;
           cout<<" vteam"<<" ["<<vteam;
           cout<<"]";
           cout<<vs;dwn=1;yd=10;

           if (beginp=='H') team='V';
           else             team='H';
           break;
       }
       case(4): strcpy(qtrs,"th");break;

我认为很难获得序数后缀...您基本上必须编写一个使用开关来测试数字并添加后缀的函数。

语言没有理由在内部提供此功能,尤其是在特定于区域设置的情况下。

在编写代码量方面,您可以比该链接做得更好,但是您必须为此编写一个函数...


推荐阅读

    linux转换字符集命令?

    linux转换字符集命令?,系统,名称,时间,位置,服务,文件,字符集,命令,格式,以

    linux命令匹配数字?

    linux命令匹配数字?,数字,档案,位置,环境,名字,较大,系统,权限,命令,用户,Lin

    linux权限数字命令?

    linux权限数字命令?,数字,系统,地址,权限,命令,标准,情况,管理,基础,文件,lin

    linux地址转换命令是?

    linux地址转换命令是?,地址,系统,代码,密码,网络,信息,服务,电脑,设备,报告,

    linux命令时间转换?

    linux命令时间转换?,时间,系统,命令,信息,国家,大陆,概念,终端,时区,时分,Lin

    linux排序数字命令?

    linux排序数字命令?,标准,数字,单位,情况,系统,信息,命令,文件,顺序,参数,lin

    linux命令不能打数字?

    linux命令不能打数字?,工作,密码,数字,地址,电脑,系统,命令,设备,下来,预期,

    linux命令时间戳转换?

    linux命令时间戳转换?,时间,系统,地址,信息,工作,代码,标准,工具,形态,命令,l

    linux转换行列命令?

    linux转换行列命令?,工作,地址,信息,网络,系统,命令,标准,电脑,环境,目录,lin

    linux地址转换命令?

    linux地址转换命令?,地址,系统,代码,信息,设备,网络,服务,密码,状态,报告,lin

    linux生成数字命令?

    linux生成数字命令?,密码,数字,设备,环境,名称,时间,单位,地址,信息,系统,求

    转换格式命令linux?

    转换格式命令linux?,系统,情况,地址,命令,设备,工作,基础,发行,工具,数据,Lin

    linux转换图形命令?

    linux转换图形命令?,系统,工作,密码,工具,地址,命令,地方,环境,软件,信息,Lin

    linux时间转换命令?

    linux时间转换命令?,时间,系统,国家,大陆,命令,信息,标准,电脑,时区,终端,lin

    linux命令模式转换?

    linux命令模式转换?,工作,系统,数据,标准,命令,地址,信息,模式,设备,密码,lin

    linux转换盘符命令?

    linux转换盘符命令?,管理,系统,电脑,数据,情况,工作,磁盘,分区,工具,环境,怎

    linux文件转换的命令?

    linux文件转换的命令?,系统,情况,工作,命令,基础,发行,名称,信息,文件,格式,

    python之转换大小写

    python之转换大小写,预期,数字,培训,字符串,方法,大小写,结果,下面,不得而