You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
371 B

  1. package humanize
  2. import "strconv"
  3. // Ordinal gives you the input number in a rank/ordinal format.
  4. //
  5. // Ordinal(3) -> 3rd
  6. func Ordinal(x int) string {
  7. suffix := "th"
  8. switch x % 10 {
  9. case 1:
  10. if x%100 != 11 {
  11. suffix = "st"
  12. }
  13. case 2:
  14. if x%100 != 12 {
  15. suffix = "nd"
  16. }
  17. case 3:
  18. if x%100 != 13 {
  19. suffix = "rd"
  20. }
  21. }
  22. return strconv.Itoa(x) + suffix
  23. }