Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created March 21, 2018 19:27
Show Gist options
  • Save vialyx/00c791ff234a89c865fbd05deacc8e03 to your computer and use it in GitHub Desktop.
Save vialyx/00c791ff234a89c865fbd05deacc8e03 to your computer and use it in GitHub Desktop.
let number = NSNumber(value: 1231.90)
let numberFormatter = NumberFormatter()
guard let formattedNumber = numberFormatter.string(from: number) else { fatalError() }
print("Formatted rounded Number: \(formattedNumber)")
// Formatted rounded Number: 1232
numberFormatter.numberStyle = .currency
guard let formattedCurrency = numberFormatter.string(from: number) else { fatalError() }
print("Formatted currency Number: \(formattedCurrency)")
// Formatted currency Number: $1,231.90
numberFormatter.numberStyle = .currencyPlural
guard let pluralCurrency = numberFormatter.string(from: number) else { fatalError() }
print("Formatted pluralCurrency Number: \(pluralCurrency)")
// Formatted pluralCurrency Number: 1,231.90 US dollars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment