Why doesn't SwiftUI TextField currency format seem to work for Euros (EUR)?

SwiftUI's TextField's format parameter accepts a ParseableFormatStyle of .currency. This is a super useful improvement for what I imagine is a really common use case. But while it mostly works seamlessly for me, for some reason it doesn't seem to work if the currency is set to Euros (EUR).

If the currency is set to EUR as in my example below, no matter what you enter in the TextField, the value resolves to 0 formatted as a localised representation of EUR 0.00. It's as if the ParseableFormatStyle doesn't recognise any input as being a valid format of EUR. If you change the currency from EUR to any of the other currencies I've tried it works fine.

Note: There is another issue I've encountered with the .currency format of TextField which I've written about in a separate post which you can find here.

import SwiftUI

struct ContentView: View {
	@State private var amount = Decimal()
	@State private var currency: Currency = .EUR // change this to any other currency to get different (and expected) results

    var body: some View {
CurrencyAmount(
	title: "Some label",
	amount: $amount,
	currency: $currency)
    }
}

struct CurrencyAmount: View {
	let title: String
	@Binding var amount: Decimal
	@Binding var currency: Currency
	let prompt: String = ""

	var body: some View {
		TextField(
			title,
			value: $amount,
			format: .currency(code: currency.rawValue),
			prompt: Text(prompt))
		}
	}
}

enum Currency: String, CaseIterable, Identifiable {
	case AUD, CAD, EUR, GBP, NZD, USD
	var id: String { self.rawValue }
}

Same here. Also tried "Barbados Dollars" (BBD), same result as with Euros. Only if I empty the whole field, including the currency sign, data entry is possible (and gets formatted correctly with the right currency).

USD works fine.

Any further insights on this?

I have the exact same problem with GBP too. I haven't tried too many currencies, but it works perfectly if I just change GBP to USD below.

TextField("Total Here", value: $totalValue,  format: .currency(code: "GBP"))
.keyboardType(.decimalPad)

When set to USD, I can enter values in the TextField with commas and the currency symbol, and my input updates as I type (as long as the input is valid - if it isn't, no action occurs). This is the precise behaviour I want.

The minute I change the currency format to GBP (or EUR), I have to enter a "pure" number only, e.g., 179019.18. No input like "£170,019.18" works.

I'm new to Swift UI, but is there a way around this?

Exactly. the issue is the sign before (ie: €). Then I just deleted it and make the sign in label

I'm using XCode 15.0.1 (Swift 5.9)

I tried other TextField formats (ParseableFormatStyle). I couldn't get them to work except USD currency. According to Apple's documentation, it is a "Beta Software". Why is it in the production version?

For the .percent format, I think the TextField is expecting a binding variable with a Decimal type. However, I can't even declare a simple Decimal type variable. Here is an example

private var pct2: Decimal = Decimal(1)

XCode shows an "Argument passed to call that takes no arguments". That's strange. According to Apple's documentation, Decimal should have an Int initializer.

If the TextField format property is a functional feature, perhaps the documentation should provide more information and examples.

I ended up using the TextField formatter property with a NumberFormatter. for all number input, such as percent, currency, and number. I haven't type of Formatter yet. Just like @Hean20, I put the currency symbol and percentage sign outside the TextField.

Why doesn't SwiftUI TextField currency format seem to work for Euros (EUR)?
 
 
Q