[英]Getting NSNumber from financial string
I have this extension to get finical string from Double
我有这个扩展来从
Double
获取最终字符串
extension Double {
static let formatter = NumberFormatter()
var financialString: String? {
Double.formatter.numberStyle = .decimal
Double.formatter.maximumFractionDigits = 2
Double.formatter.minimumFractionDigits = 2
if let result = Double.formatter.string(for: self) {
return result
} else {
return nil
}
}
}
I'm getting 1,001.00 text value from 1001 which is right.我从 1001 得到 1,001.00 文本值,这是正确的。 One thing that I want to get 1001 back from 1,001.00 now.
我现在想从 1,001.00 取回 1001 的一件事。
Is there a way to get NSNumber from financial string?有没有办法从金融字符串中获取 NSNumber?
You could filter the string using a character set and only get the numbers and decimal point back.您可以使用字符集过滤字符串,只获取数字和小数点。 Then convert that string to a Double.
然后将该字符串转换为 Double。
let characterSet = Set(".0123456789")
Double("$1,001.00".filter(characterSet.contains))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.