SwiftUI - 显示双舍入到小数点后两位

SwiftUI - Display Double rounded to 2 decimal places

我有一个美元(货币)金额,我试图在 Text 元素中使用字符串插值来显示它。目前,它显示如 9.9900000。我希望该值仅显示为 9.99。

struct ContentView: View {
var productPrice: Double = 9.99

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