Display currency in SwiftUI

To display Currency properly formatted in SwiftUI you have to do the following

Define the value of Currency as a double

@State private var unitPrice = 0.0

For a TextField, write this:

TextField("Unit Price: ", value: $unitPrice , format: .currency(code: Locale.current.currency?.identifier ?? "USD"))

For a Text, write this:

Text(unitPrice, format: .currency(code: Locale.current.currency?.identifier ?? “USD”))

Here’s how the entire code looks like:

import SwiftUI

struct ContentView: View {
    
    @State private var unitPrice = 0.0
    @FocusState private var isAmountFocussed: Bool
    
    
    var body: some View {
        Form{
            Section{
                Text("Enter the Unit Price")
                TextField("Unit Price: ", value: $unitPrice , format: .currency(code: Locale.current.currency?.identifier ?? "USD"))
                    .keyboardType(.decimalPad)
                    .focused($isAmountFocussed)
            }
                Section{
                Text("The Unit Price is:")
                Text(unitPrice, format: .currency(code: Locale.current.currency?.identifier ?? "USD"))
            }
        }
    }
}


#Preview {
    ContentView()
}

And here is how it looks in Xcode

If you found this useful, give it a share on your social media platforms.

Follow me on Twitter/X at @DevPaulC