Skip to content

Instantly share code, notes, and snippets.

@devjangir
Last active April 2, 2017 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devjangir/a9166881a396f40a57e7eb23e48d7087 to your computer and use it in GitHub Desktop.
Save devjangir/a9166881a396f40a57e7eb23e48d7087 to your computer and use it in GitHub Desktop.
Use NSNumberFormatter in Swift to Make Currency Numbers Easy to Read
var currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = NumberFormatter.Style.currency
// localize to your grouping and decimal separator
// set locale to Spanish
currencyFormatter.locale = Locale(identifier: "eu_ES")
var priceString = currencyFormatter.string(from: 12.50)
print(priceString) //output : ("12,50 €")
// set locale to US
currencyFormatter.locale = Locale(identifier: "en_US")
priceString = currencyFormatter.string(from: 12.50)
print(priceString) //output : ("$12.50")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment