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

时间:2019-10-15 23:55:22

标签: double swiftui string-interpolation

我有一个要尝试使用Text元素中的字符串插值显示的美元(货币)金额。当前,显示为9.9900000。我希望该值仅显示为9.99。

struct ContentView: View {
var productPrice: Double = 9.99

    var body: some View {
       Text("\(productPrice)")
    }
}

1 个答案:

答案 0 :(得分:4)

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