Posted By

jmcd on 05/06/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

paelgr
umang_nine


Ordinal suffix of an integer


 / Published in: C#
 

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

  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  

You need to login to post a comment.