Swift 4 String Formatter转换为货币

时间:2018-10-18 09:33:37

标签: swift

我有字符串99897.05,我想像这样转换它99 897.05 例: 输入:8121.1 输出8 121.1

输入111111.11 输出111 111.11

1 个答案:

答案 0 :(得分:2)

您需要的是currencyGroupingSeparator

let amount = 99897.05
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyGroupingSeparator = " "

formatter.string(for: amount) //"$99 897.05"

您可以根据需要进行更改:

formatter.currencyGroupingSeparator = ""
formatter.string(for: amount) //"$99897.05"