解析带有货币符号和值的字符串
我正在尝试从 Excel 文件中解析输入字符串列表,该文件可以具有“货币”值,并且可以是任何货币。例如
- $200
- £300
- €200
- CA$300
解析货币符号和数值的最佳方法是什么?我尝试使用 NumberFormatter
执行此操作,但它不适用于“欧元”或“CAD”值。
这是我的代码:
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .currency
currencyFormatter.maximumFractionDigits = 2
let trimmedString = String(currencyString.filter { String($0).rangeOfCharacter(from: CharacterSet(charactersIn: "0123456789.,")) == nil }).trimmingCharacters(in: .whitespacesAndNewlines)
if trimmedString.count > 0 && Locale.current.currencySymbol != trimmedString {
// Currency symbol is *not* local currency, so lookup the locale for it
let allLocales = Locale.availableIdentifiers.map({Locale(identifier: $0)})
if let localeForSymbol = allLocales.filter({$0.currencySymbol == trimmedString}).first {
currencyFormatter.locale = localeForSymbol
}
}
if let numberValue = currencyFormatter.number(from: currencyString) {
print ("\(NSDecimalNumber(decimal: numberValue.decimalValue))")
}
我在这里犯了什么错?或者如果不使用一些正则表达式这是不可能的?
I am trying to parse a list of input strings from an Excel file that can have a 'currency' value, and it could be in any currency. For e.g.
- $200
- £300
- €200
- CA$300
What's the best way to parse out the currency symbol and the numeric value? I'm trying to do this with a NumberFormatter
but it doesn't work for the 'euro' or the 'CAD' value.
Here is my code:
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .currency
currencyFormatter.maximumFractionDigits = 2
let trimmedString = String(currencyString.filter { String($0).rangeOfCharacter(from: CharacterSet(charactersIn: "0123456789.,")) == nil }).trimmingCharacters(in: .whitespacesAndNewlines)
if trimmedString.count > 0 && Locale.current.currencySymbol != trimmedString {
// Currency symbol is *not* local currency, so lookup the locale for it
let allLocales = Locale.availableIdentifiers.map({Locale(identifier: $0)})
if let localeForSymbol = allLocales.filter({$0.currencySymbol == trimmedString}).first {
currencyFormatter.locale = localeForSymbol
}
}
if let numberValue = currencyFormatter.number(from: currencyString) {
print ("\(NSDecimalNumber(decimal: numberValue.decimalValue))")
}
What am I getting wrong here? Or is this not possible without using some regex expressions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以尝试这个“...解析出货币符号和数值”:
you could try this "...to parse out the currency symbol and the numeric value":
@workingdog 有一个有效的答案。另一种方法,也是我一直追求的方法,是使用正则表达式,其中 3 个简单的正则表达式可用于识别任何非数字前导字符、数字字符和任何非数字后缀字符:
然后可以将它们与 String 一起使用
.range(of: options:)
使用.regularExpression
选项获取元素的范围,然后使用它来提取相关的 SubString。作为一个非常快速的演示:
这提供了
.padded(to:)
的输出,它是 String 上的一个实用程序扩展,它用任何字符将字符串的后部填充到指定的长度。
@workingdog has a valid answer. An alternative, and one I always reach for, is to use regex, where 3 simple regex can be used to identify any non-numeric leading characters, the numeric characters, and any non-numeric postfix characters:
These can then be used with String's
.range(of: options:)
using the.regularExpression
option to get the element's range, and then use that to extract the relevant SubString.As a very quick demo of this in action:
This provides an output of
.padded(to:)
is a utility extension on String that pads the rear of a string to a specified length with any character.