我的currencyFormatter
语言环境为se_SV
。
var currencyFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.currencySymbol = ""
formatter.locale = Locale(identifier: "se_SV")
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.numberStyle = .currencyAccounting
formatter.isLenient = true
return formatter
}()
我正在将NSNumber
转换为String
。
let firstString = currencyFormatter.string(from: NSNumber(value: 22222222.50)) // "22 222 222,50 "
而且我手动创建的String
与firstString
相同。
let secondString = "22 222 222,50 "
为什么当我检查firstString == secondString
时是否收到false
?
答案 0 :(得分:4)
print(Array(firstString!.unicodeScalars))
// ["2", "2", "\u{00A0}", "2", "2", "2", "\u{00A0}", "2", "2", "2", ",", "5", "0", "\u{00A0}"]
print(firstString!.replacingOccurrences(of: "\u{A0}", with: " ") == secondString)
// true
显示数字格式化程序用“非中断空格”U+00A0
分隔这些组。这可以防止数字被分割
多行文字中的行。