Is there a way to use NSNumberFormatter to get the \'th\' \'st\' \'nd\' \'rd\' number endings?
EDIT:
Looks like it does not exist. Here\'s what I\'m using.>
Here's a compact Swift extension suitable for all integer types:
extension IntegerType {
func ordinalString() -> String {
switch self % 10 {
case 1...3 where 11...13 ~= self % 100: return "\(self)" + "th"
case 1: return "\(self)" + "st"
case 2: return "\(self)" + "nd"
case 3: return "\(self)" + "rd"
default: return "\(self)" + "th"
}
}
}
Example usage:
let numbers = (0...30).map { $0.ordinalString() }
print(numbers.joinWithSeparator(", "))
Output:
0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th