I have written an extension function to determine if a month day/number is 1st/31st, 2nd/22nd, 3rd or 4th....30th.
I would like to find out if there is a better/shorter or elegant way I could've written it? My function:
/// <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();
}