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

Is there a way to format a number in cocoa for its ordinal string 1, 2, 3, etc. Is it preferable to localize? Thank you Jose

+3
source share
3 answers

ADNOrdinalNumberFormatter does ordinal formatting, but unfortunately, localization is not known.

+1
source

Like iOS 9.0+, macOS 10.11+, tvOS 9.0+, and watchOS 2.0+, this is in the Cocoa standard library:

Swift

let ordinalFormatter = NumberFormatter()
ordinalFormatter.numberStyle = .ordinal

print(ordinalFormatter.string(from: NSNumber(value: 3))) // 3rd

Goal c

NSNumberFormatter * ordinalFormatter = [NumberFormatter new];
ordinalFormatter.numberStyle = NSNumberFormatterOrdinalStyle;

NSLog([ordinalFormatter stringFromNumber: @3]); // 3rd
+2
source

All Articles