is there a way to format a number in co开发者_JAVA技巧coa to its ordinal string 1st, 2nd, 3rd, etc... Preferably localized aware? Thank you, Jose
As of iOS 9.0+, macOS 10.11+, tvOS 9.0+, and watchOS 2.0+, this is in Foundation:
Swift
let ordinalFormatter = NumberFormatter()
ordinalFormatter.numberStyle = .ordinal
print(ordinalFormatter.string(from: NSNumber(value: 3))) // 3rd
Objective C
NSNumberFormatter * ordinalFormatter = [NumberFormatter new];
ordinalFormatter.numberStyle = NSNumberFormatterOrdinalStyle;
NSLog([ordinalFormatter stringFromNumber: @3]); // 3rd
ADNOrdinalNumberFormatter does ordinal formatting but is unfortunately not localization aware.
TTTOrdinalNumberFormatter from FormatterKit does ordinal formatting in the following languages:
- Chinese
- Dutch
- English
- French
- German
- Italian
- Irish
- Japanese
- Portuguese
- Spanish
精彩评论