Last active
January 21, 2016 05:50
-
-
Save huntermonk/c81b1adaed5b3c716d23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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