Swift 3 and NumberFormatter (.currency) == ¤?

Xcode 8.0 (8A218a) GM
Target : iOS 10 (Swift 3)

Consider the following code:

let number = NSDecimalNumber(decimal: 22.4) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .currency numberFormatter.locale = Locale.current let result = numberFormatter.string(from: number) print(result!) 

Result:

 ¤22.40 

(I don't know what ¤ means.)

But if I initialize the locale, for example:

 numberFormatter.locale = Locale(identifier: "en_US") 

The result will be:

 $22.40 

... this is what I expect in the first place.

Please note that this works on the tho playground:

enter image description here

The problem seems to only happen on devices / simulators with the launch of Xcode.
(I tested on two different macOS - at my workplace and at home.)

Any ideas on what's going on?

+8
ios ios10 swift3 locale nsnumberformatter
source share
4 answers

Answering my own question: recreate the project.: \

The project was launched in Xcode 7 + Swift 2, and then "migrated" to Xcode 8 + Swift 3 (manually). Something must have gone wrong. I do not know that (and I do not want to know).

It was not a huge project, so I spent about 40 minutes moving files. If it were a big project, I would already be f .... d.

Xcode pls.

+1
source share

I have the same problems and I solve this below code. Hope this is helpful

 func formatCurrency(value: Double) -> String { let formatter = NumberFormatter() formatter.numberStyle = .currency formatter.maximumFractionDigits = 2 formatter.locale = Locale(identifier: Locale.current.identifier) let result = formatter.string(from: value as NSNumber) return result! } 
+12
source share

This happened to me in Simulator, it turned out that it was a mistake in setting up the circuit. Issue the following steps:

  • Click "Set Active Scheme" (click right to the left of the simulator type, for example, iPhone 7 Plus, etc.).
  • Click Change Schema ...
  • Click the Run button on the left.
  • Set Application Area to the desired area
  • Set the Application Language to the desired language
  • Try to run the application again, you will no longer need to display the "¤" symbol, and instead it should display the correct currency symbol.
+1
source share

I think your device language is set to Chinese so the symbol denoted by the currency symbol

0
source share

All Articles