Format number as ranking position [duplicate]
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"
}
精彩评论