Skip to content

Instantly share code, notes, and snippets.

@dbettermann
Created January 31, 2015 22:48
Show Gist options
  • Save dbettermann/03f295528843d5425c76 to your computer and use it in GitHub Desktop.
Save dbettermann/03f295528843d5425c76 to your computer and use it in GitHub Desktop.
String Extension - Ordinal number formatting
extension String {
static func ordinalNumberFormat(number: Int) -> String {
var ending = "th"
let ones = number % 10
let tens = ((number / 10) % 10) as Int
if tens != 1 { // tens == 1 will always end in "th"
switch ones {
case 1:
ending = "st"
case 2:
ending = "nd"
case 3:
ending = "rd"
default:
println("String.ordinalNumberFormat(\(number)) defaulting to \(ending)")
}
}
return "\(number)\(ending)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment