SwiftUI - Display Double rounded to 2 decimal places

Viewed 10611

I have a dollar ( monetary) amount that I'm trying to display using string interpolation in a Text element. Currently, it displays such as 9.9900000. I want the value just to display as 9.99.

struct ContentView: View {
var productPrice: Double = 9.99

    var body: some View {
       Text("\(productPrice)")
    }
}
1 Answers
struct ContentView: View {
var productPrice: Double = 9.99
    var body: some View {
       Text("\(productPrice, specifier: "%.2f")")
    }
}
Related