开发者

Formatting a number to ordinal number? (1st, 2nd, etc..)

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜