Swift。如何设置自定义数字格式?带有 space 千位分隔符和点后两位数

Swift. How to set custom number format? With space thouthand separator and two digits after point

我需要用 space 和点后两位数分开的数字。

let d1: Double = 20000000.0
let d2: Double = 1.2345

我想看:

let s1 = String(format: "????", d1) //20 000 000.00
let s2 = String(format: "????", d2) //1.23

怎么做?

let d1: Double = 20000000.0
let d2: Double = 1.2345

let formatter = NumberFormatter()
formatter.groupingSeparator = " "
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.decimalSeparator = "." // Default separator is dependent to the current local.

print(formatter.string(for: d1)) // 20 000 000.00
print(formatter.string(for: d2)) // 1.23