到1、2、3、4(月天)

时间:2019-11-13 19:53:35

标签: c# .net

我写了一个扩展函数来确定月日/数字是1月31日,2月22日,3日还是4日... 30日。

我想知道是否可以编写一种更好/更短或更优雅的方式?我的功能:

/// <summary>
/// To 1st, 2nd, 3rd, 4th (Month days)
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public static string To1st2nd3rd4th( this double n )
{
    // nFirst
    if ( new double[] { 1, 21, 31 }.Contains( n ) )
    {
        return $"{n}st";
    }

    // nSecond
    if ( new double[] { 2, 22 }.Contains( n ) )
    {
        return $"{n}nd";
    }

    // nThird
    if ( n == 3 )
    {
        return $"{n}rd";
    }

    //n-th/ n > 3
    if ( n > 3 )
    {
        return $"{n}th";
    }

    return n.ToString();
}

3 个答案:

答案 0 :(得分:1)

您的解决方案几乎没有问题。

首先,为什么一天(看来是日期)必须为double?所以我会在这里使用int

第二,您的if语句可以为if-else。您必须为其添加一个负数或零的大小写。

但是,这有一个问题,无法处理诸如33之类的数字(请确保每月的第33天没有,但是功能应该是自足的)。

我宁愿使用Humanizer库,它可以做到这一点以及更多。

您可以获得NuGet软件包,完成后就很简单:

Console.WriteLine("{0} ==> {1}", 1, 1.Ordinalize());
Console.WriteLine("{0} ==> {1}", 21, 21.Ordinalize());
Console.WriteLine("{0} ==> {1}", 31, 31.Ordinalize());
Console.WriteLine("{0} ==> {1}", 33, 33.Ordinalize());
Console.WriteLine("{0} ==> {1}", 2, 22.Ordinalize());
Console.WriteLine("{0} ==> {1}", 3, 3.Ordinalize());
Console.WriteLine("{0} ==> {1}", 4, 4.Ordinalize());
Console.WriteLine("{0} ==> {1}", 55, 55.Ordinalize());
Console.WriteLine("{0} ==> {1}", 1237, 1237.Ordinalize());
Console.WriteLine("{0} ==> {1}", 0, 0.Ordinalize());

输出

  

1 ==> 1st

     

21 ==> 21st

     

31 ==> 31st

     

33 ==> 33rd

     

2 ==> 22nd

     

3 ==> 3rd

     

4 ==>第四

     

55 ==> 55th

     

1237 ==> 1237th

     

0 ==>第0

答案 1 :(得分:0)

  private string NumberSuffix(double n)
    {
        var below100 = n % 10;
        var above100 = n % 100;

        if (below100 == 1 && above100 != 11)
        {
            return n + "st";
        }
        if (below100 == 2 && above100 != 12)
        {
            return n + "nd";
        }
        if (below100 == 3 && above100 != 13)
        {
            return n + "rd";
        }
        return n + "th";
    }

答案 2 :(得分:0)

我认为这可以为您通用:

        static void Main(string[] args)
    {
        Console.WriteLine(String.Join(" ", Enumerable.Range(0, 24).Select(NumberSuffix)));
        Console.WriteLine(String.Join(" ", Enumerable.Range(250, 24).Select(NumberSuffix)));
        Console.WriteLine(String.Join(" ", Enumerable.Range(10001, 24).Select(NumberSuffix)));

        Console.ReadKey();
    }

    public static string NumberSuffix2(int n)
    {
        //                           0     1     2     3     4     5     6     7     8     9      10    11    12
        string[] nth = new string[] {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th"};

        int n2 = Math.Abs(n);

        return n + nth[(n2 % 100) % (n2 % 100 > 13 ? 10 : 13)];
    }

输出为:

0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd

第250名251名252名253名254名255名256名257名258名259名260名261名262名263名264名265名266名267名268名269名269名270名271名272名273名

0001st 10002nd 10003rd 10004th 10005th 10006th 10007th 10008th 10009th 10010th 10011th 10012th 10013th 10014th 10015th 10016th 10017th 10018th 10019th 10020th 10021st 10022th 10023rd 10024th