开发者

Format number as ranking position [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

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

Is there开发者_运维知识库 anything already built into C# that formats a number as a ranking position?

By "ranking position" is mean one of ["st","nd,"rd","th"] eg : (1st, 2nd, 3rd, 4th).

I know it'd be easy to write an extension for this, I'm just wondering if there is already something in the language to cater.

Cheers


No, there is nothing built-in for this purpose. Not sure if its relevant, but consider cultural differences if you span languages/cultures.


I've been corrected. Please see the duplicate questions as described in the comments above.

Not out-of-the-box; however, as you mentioned, an extension is pretty easy.

public static string ConvertToRankingPosition(this int value)
{
    var digit = value % 10;
    return value != 11 && digit == 1 : value + "st" :
           value != 12 && digit == 2 : value + "nd" :
           value != 13 && digit == 3 : value + "rd" :
           value + "th"
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜