Created
January 31, 2015 22:48
-
-
Save dbettermann/03f295528843d5425c76 to your computer and use it in GitHub Desktop.
String Extension - Ordinal number formatting
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 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