Ordinal suffix of an integer


/ Published in: C#
Save to your folder(s)

The switches are a little ugly, but they are quick.


Copy this code and paste it in your HTML
  1. public static class IntExtensions
  2. {
  3. public static string GetOrdinalSuffix(this int @this)
  4. {
  5. switch (@this % 100)
  6. {
  7. case 11:
  8. case 12:
  9. case 13:
  10. return "th";
  11. default:
  12. switch (@this % 10)
  13. {
  14. case 1:
  15. return "st";
  16. case 2:
  17. return "nd";
  18. case 3:
  19. return "rd";
  20. default:
  21. return "th";
  22. }
  23. }
  24. }
  25. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.