I'm using Swift's NumberFormatter with currency numberStyle. I set the following properties:
let value: Double = 345.23
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 0
formatter.locale = Locale(identifier: "en_US")
formatter.currencyCode = "USD"
print(formatter.string(from: NSNumber(value: value))) //Will print $345 as expected
If I change the currency and print again, I get two decimals. Why?
formatter.currencyCode = "EUR"
print(formatter.string(from: NSNumber(value: value))) //Will print €345.23 with two decimals
Is this supposed to happen or is it a bug? If I change the locale to something else, like "fr_FR", and then back again to "en_US" it will print with 0 decimals. Please see attached result from XCTestCase. Only when locale has been modified will the currency have the expected number of decimals.
UPDATE: It works for iOS 13.3, but fails for iOS 12.0 and 11.0.1