Is there an easy way in C# to create Ordinals for a number? For example:
- 1 returns 1st
- 2 returns 2nd
- 3 returns 3rd
- ...etc
Can this be done through String.Format()
or are there any functions available to do this?
Is there an easy way in C# to create Ordinals for a number? For example:
Can this be done through String.Format()
or are there any functions available to do this?
Similar to Ryan's solution, but even more basic, I just use a plain array and use the day to look up the correct ordinal:
I have not had the need, but I would assume you could use a multidimensional array if you wanted to have multiple language support.
From what I can remember from my Uni days, this method requires minimal effort from the server.
Here is the DateTime Extension class. Copy, Paste & Enjoy
public static class DateTimeExtensions {
Result :
9th October 2014
If anyone looking for one liner :p
I rather liked elements from both Stu's and samjudson's solutions and worked them together into what I think is a usable combo:
This page gives you a complete listing of all custom numerical formatting rules:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
As you can see, there is nothing in there about ordinals, so it can't be done using String.Format. However its not really that hard to write a function to do it.
Update: Technically Ordinals don't exist for <= 0, so I've updated the code above. Also removed the redundant ToString() methods.
Also note, this is not internationalised. I've no idea what ordinals look like in other languages.