Skip to content

Instantly share code, notes, and snippets.

@robtimp
Last active January 13, 2018 18:25
Show Gist options
  • Save robtimp/f5c9ebf6ed99631f1002418ee660c5f5 to your computer and use it in GitHub Desktop.
Save robtimp/f5c9ebf6ed99631f1002418ee660c5f5 to your computer and use it in GitHub Desktop.
func ordinal(forNumber: Int) -> String {
let tens = (number / 10) % 10
let ones = number % 10
let suffix: String
switch (tens, ones) {
case (1, _):
suffix = "th"
case (_, 1):
suffix = "st"
case (_, 2):
suffix = "nd"
case (_, 3):
suffix = "rd"
default:
suffix = "th"
}
return "\(number)\(suffix)"
}
@skliarov
Copy link

Thanks, this was useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment