解析带有货币符号和值的字符串

发布于 2025-01-16 21:22:30 字数 1149 浏览 4 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2025-01-23 21:22:30

你可以尝试这个“...解析出货币符号和数值”:

let currencyString = "CA$300"

let valueString = currencyString.filter {
    CharacterSet(charactersIn: "0123456789.,").isSuperset(of: CharacterSet(charactersIn: String($0)))
}.trimmingCharacters(in: .whitespacesAndNewlines)
print("---> valueString: \(valueString) ")

let symbol = currencyString.replacingOccurrences(of: valueString, with: "")
print("---> symbol: \(symbol)")

you could try this "...to parse out the currency symbol and the numeric value":

let currencyString = "CA$300"

let valueString = currencyString.filter {
    CharacterSet(charactersIn: "0123456789.,").isSuperset(of: CharacterSet(charactersIn: String($0)))
}.trimmingCharacters(in: .whitespacesAndNewlines)
print("---> valueString: \(valueString) ")

let symbol = currencyString.replacingOccurrences(of: valueString, with: "")
print("---> symbol: \(symbol)")
薄荷港 2025-01-23 21:22:30

@workingdog 有一个有效的答案。另一种方法,也是我一直追求的方法,是使用正则表达式,其中 3 个简单的正则表达式可用于识别任何非数字前导字符、数字字符和任何非数字后缀字符:

let prefixRegex = #"^[^0-9]+"#
let numRegex = #"[0-9]+"#
let postfixRegex = #"[^0-9]+$"#

然后可以将它们与 String 一起使用.range(of: options:) 使用 .regularExpression 选项获取元素的范围,然后使用它来提取相关的 SubString。

作为一个非常快速的演示:

["$20", "£300", "€200", "CA$300", "798xyz", "$123abc"]
   .forEach{string in
      var components: [Range<String.Index>?] = []
      
      components.append( string.range(of: prefixRegex, options: .regularExpression))
      components.append( string.range(of: numRegex, options: .regularExpression))
      components.append( string.range(of: postfixRegex, options: .regularExpression))
      
      print(components
         .map{ $0 != nil ? String(string[$0!]).padded(to: 8) : String(repeating: " ", count: 8) }
         .joined(separator: "")
      )
   }

这提供了 .padded(to:) 的输出

$       20              
£       300             
€       200             
CA$     300             
        798     xyz     
$       123     abc  

,它是 String 上的一个实用程序扩展,它用任何字符将字符串的后部填充到指定的长度。

extension String {
   func padded(to paddedTo :Int, with padding: String = " ") -> Self {
      while count < paddedTo {
         return (self + padding).padded(to: paddedTo, with: padding)
      }
      return self
   }
}

@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:

let prefixRegex = #"^[^0-9]+"#
let numRegex = #"[0-9]+"#
let postfixRegex = #"[^0-9]+
quot;#

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:

["$20", "£300", "€200", "CA$300", "798xyz", "$123abc"]
   .forEach{string in
      var components: [Range<String.Index>?] = []
      
      components.append( string.range(of: prefixRegex, options: .regularExpression))
      components.append( string.range(of: numRegex, options: .regularExpression))
      components.append( string.range(of: postfixRegex, options: .regularExpression))
      
      print(components
         .map{ $0 != nil ? String(string[$0!]).padded(to: 8) : String(repeating: " ", count: 8) }
         .joined(separator: "")
      )
   }

This provides an output of

$       20              
£       300             
€       200             
CA$     300             
        798     xyz     
$       123     abc  

.padded(to:) is a utility extension on String that pads the rear of a string to a specified length with any character.

extension String {
   func padded(to paddedTo :Int, with padding: String = " ") -> Self {
      while count < paddedTo {
         return (self + padding).padded(to: paddedTo, with: padding)
      }
      return self
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文