Skip to content

Instantly share code, notes, and snippets.

@huntermonk
Last active January 21, 2016 05:50
Show Gist options
  • Save huntermonk/c81b1adaed5b3c716d23 to your computer and use it in GitHub Desktop.
Save huntermonk/c81b1adaed5b3c716d23 to your computer and use it in GitHub Desktop.
extension Int {
var ordinal:String {
get {
if self == 0 {
return "0"
}
switch self {
case 11:
return "11th"
case 12:
return "12th"
case 13:
return "13th"
default: break
}
let characters = String(self).characters
let lastDigit = Int(String(characters.last!))!
let suffix:String
switch lastDigit {
case 0:
suffix = "th"
case 1:
suffix = "st"
case 2:
suffix = "nd"
case 3:
suffix = "rd"
case 4, 5, 6, 7, 8, 9:
suffix = "th"
default:
suffix = ""
}
return "\(self)\(suffix)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment