I understand NumberFormatter's default behaviour is to include the country code as part of the symbol if you are not in that country, for example, this test passes:
func testUSDFromInsideUS() {
let formatter = NumberFormatter()
formatter.currencyCode = "USD"
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "en_US")
XCTAssertEqual("$100.00", formatter.string(from: NSNumber(100)))
}
However, when the locale is changed to a country that isn't the US, for example GB the number formatter prepends US to the $ sign:
func testUSDFromOutsideUS() {
let formatter = NumberFormatter()
formatter.currencyCode = "USD"
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "en_GB")
XCTAssertEqual("$100.00", formatter.string(from: NSNumber(100))) // <-- fails because the value is "US$100.00", but I'd like it to be "$100.00".
}
I had a look at NumberFormatter docs but couldn't see a property to disable this behaviour. I understand it's to avoid confusion with other $ currencies.
Do I have to write my own NumberFormatter subclass for this, or is there a simple way do have this behaviour?
This little hack should do the job