代码之家  ›  专栏  ›  技术社区  ›  Matt Mitchell

在.NET中,有没有一种简单的方法可以获得数字的“st”、“nd”、“rd”和“th”结尾?[复制品]

  •  61
  • Matt Mitchell  · 技术社区  · 16 年前

    这个问题已经有了答案:

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

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

    This link 在编写自己的函数时,有一个关于基本原理的坏例子,但是我更好奇的是,是否有我所缺少的内在能力。

    解决方案

    斯科特·汉塞尔曼的回答是公认的,因为它直接回答了这个问题。

    但是,有关解决方案,请参见 this great answer .

    11 回复  |  直到 10 年前
        1
  •  54
  •   Scott Hanselman    16 年前

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

        2
  •  82
  •   nickf    16 年前

    这是一个比你想象的简单得多的函数。尽管可能已经存在一个.NET函数,但是下面的函数(用PHP编写)可以完成这项工作。把它移植过去不应该太难。

    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;
    }
    
        3
  •  55
  •   Scott Dorman    16 年前

    @nickf:下面是c中的php函数:

    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);
    }
    
        4
  •  54
  •   Shahzad Qureshi    10 年前

    简单、干净、快速

        private static 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";
        }
    

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

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

    现在你可以打电话了

    int a = 1;
    a.DisplayWithSuffix(); 
    

    甚至直接

    1.DisplayWithSuffix();
    
        5
  •  12
  •   mezoid    11 年前

    这已经被涵盖了,但我不确定如何链接到它。这是代码截图:

        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,只需删除此关键字

    [编辑]:感谢您指出这是不正确的,这是您复制/粘贴代码的原因:)

        6
  •  8
  •   nickf    16 年前

    以下是Microsoft SQL Server函数版本:

    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
    
        7
  •  2
  •   avenmore    13 年前

    我知道这不是OP问题的答案,但是因为我发现从这个线程中提升SQL Server函数很有用,这里有一个delphi(pascal)等价物:

    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;
    

    是…,-第1,0条有意义吗?

        8
  •  0
  •   Faust    11 年前
    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";
    }
    
        9
  •  0
  •   Frank Hoffman    10 年前

    另一种味道:

    /// <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";
                }
            }
        }
    }
    
        10
  •  -3
  •   CodesInChaos    10 年前
    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;
    
        11
  •  -6
  •   Hugh Buchanan    16 年前

    我觉得序数后缀很难…基本上,您必须编写一个函数,该函数使用开关来测试数字并添加后缀。

    语言没有理由在内部提供这一点,特别是在特定于区域设置的情况下。

    当涉及到要写的代码量时,您可以做得比链接好一点,但是您必须为此编写一个函数…

    推荐文章