2

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

Community
  • 1
  • 1
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162

2 Answers2

0

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

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Thanks Jamiec. In considering cultures I'm thinking of storing the ordinal specifier in a resouce file so that we can specify specific ones for certain cultures. What do you think of this as a solution? – Jamie Dixon Aug 04 '10 at 15:54
-2

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"
}

kbrimington
  • 25,142
  • 5
  • 62
  • 74
  • needs to handle 11,12, and 13 – Woot4Moo Aug 04 '10 at 15:51
  • There appear to be some corner cases that your code doesn't incorporate. Have a look at the answers to the duplicate questions. – dtb Aug 04 '10 at 15:51
  • Ill put it here as everyone else has in all the other similar SO questions; Your method is broken as 1011 returns 1011st. – Jamiec Aug 04 '10 at 15:51