Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting input for currency with NSNumberFormatter in Swift

I am creating a budget app that allows the user to input their budget as well as transactions. I need to allow the user to enter both pence and pounds from separate text fields and they need to be formatted together with currency symbols. I have this working fine at the moment but would like to make it localised as currently it only works with GBP. I have been struggling to convert NSNumberFormatter examples from Objective-C to Swift.

My first issue is the fact that I need to set the placeholders for the input fields to be specific to the users location. Eg. Pounds and Pence, Dollars and Cents etc...

The second issue is that the values inputted in each of the text fields such as 10216 and 32 need to be formatted and the currency symbol specific to the users location needs to be added. So it would become £10,216.32 or $10,216.32 etc...

Also, I need to use the result of the formatted number in a calculation. So how can I do this without running into issues without running into issues with the currency symbol?

like image 795
user3746428 Avatar asked Jul 25 '14 16:07

user3746428


2 Answers

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) // "$123.44"  formatter.locale = Locale(identifier: "es_CL") formatter.string(from: price) // $123"  formatter.locale = Locale(identifier: "es_ES") formatter.string(from: price) // "123,44 €" 

Here's the old example on how to use it on Swift 2.

let price = 123.436  let formatter = NSNumberFormatter() formatter.numberStyle = .CurrencyStyle // formatter.locale = NSLocale.currentLocale() // This is the default formatter.stringFromNumber(price) // "$123.44"  formatter.locale = NSLocale(localeIdentifier: "es_CL") formatter.stringFromNumber(price) // $123"  formatter.locale = NSLocale(localeIdentifier: "es_ES") formatter.stringFromNumber(price) // "123,44 €" 
like image 185
NiñoScript Avatar answered Sep 29 '22 05:09

NiñoScript


Swift 3:

If you are looking for a solution that gives you:

  • "5" = "$5"
  • "5.0" = "$5"
  • "5.00" = "$5"
  • "5.5" = "$5.50"
  • "5.50" = "$5.50"
  • "5.55" = "$5.55"
  • "5.234234" = "5.23"

Please use the following:

func cleanDollars(_ value: String?) -> String {     guard value != nil else { return "$0.00" }     let doubleValue = Double(value!) ?? 0.0     let formatter = NumberFormatter()     formatter.currencyCode = "USD"     formatter.currencySymbol = "$"     formatter.minimumFractionDigits = (value!.contains(".00")) ? 0 : 2     formatter.maximumFractionDigits = 2     formatter.numberStyle = .currencyAccounting     return formatter.string(from: NSNumber(value: doubleValue)) ?? "$\(doubleValue)" } 
like image 30
Gregg Avatar answered Sep 29 '22 03:09

Gregg