NSNumberFormatter and ‘th’ ‘st’ ‘nd’ ‘rd’ (ordinal) number endings

The correct way to do this from iOS 9 onwards, is: NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; numberFormatter.numberStyle = NSNumberFormatterOrdinalStyle; NSLog(@”%@”, [numberFormatter stringFromNumber:@(1)]); // 1st NSLog(@”%@”, [numberFormatter stringFromNumber:@(2)]); // 2nd NSLog(@”%@”, [numberFormatter stringFromNumber:@(3)]); // 3rd, etc. Alternatively: NSLog(@”%@”, [NSString localizedStringFromNumber:@(1) numberStyle:NSNumberFormatterOrdinalStyle]); // 1st

How to input currency format on a text field (from right to left) using Swift?

For Swift 3. Input currency format on a text field (from right to left) override func viewDidLoad() { super.viewDidLoad() textField.addTarget(self, action: #selector(myTextFieldDidChange), for: .editingChanged) } @objc func myTextFieldDidChange(_ textField: UITextField) { if let amountString = textField.text?.currencyInputFormatting() { textField.text = amountString } } extension String { // formatting text for currency textField func currencyInputFormatting() -> String … Read more

Formatting input for currency with NSNumberFormatter in Swift

Here’s an example on how to use it on Swift 3. ( Edit: Works in Swift 5 too ) let price = 123.436 as NSNumber let formatter = NumberFormatter() formatter.numberStyle = .currency // formatter.locale = NSLocale.currentLocale() // This is the default // In Swift 4, this ^ was renamed to simply NSLocale.current formatter.string(from: price) // … Read more