SwiftUI - 显示保留两位小数的双精度浮点数

26

我有一个货币金额需要在

struct ContentView: View {
var productPrice: Double = 9.99

    var body: some View {
       Text("\(productPrice)")
    }
}
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接