【问题标题】:Extract currency from formatted amount从格式化金额中提取货币
【发布时间】:2011-11-26 20:02:08
【问题描述】:

我从网络服务中获得了格式化的金额。它可以采用不同的格式并使用不同的货币,例如

  • $ 1.10
  • € 1,10
  • 1,10 €
  • EUR 1.10(也许,我不确定我真的会遇到这个)

我想从中提取货币符号 ($),并尽可能从该符号中获取关联的 Currency 对象 (Java)。我不需要提取金额,我可以在其他地方获得。

【问题讨论】:

标签: java parsing currency


【解决方案1】:

您可以使用正则表达式来解析来自网络服务的结果。您必须过滤除数字、点和空格之外的所有字符。这是正则表达式:

String regexp = "[^0-9\\.,\\s]*";

匹配结果的第一组是货币符号(或名称,例如 EUR)。

这是我的示例方法:

public void test() throws Exception {
    String text = "2.02 $";
    String regexp = "[^0-9\\.,\\s]*";
    Pattern p = Pattern.compile(regexp);
    Matcher m = p.matcher(text);
    while (m.find()) {
        for (int i = 0; i < m.groupCount() + 1; i++)
            LOG.info(m.group(i));
    }
}

【讨论】:

  • +1 表示throws Exception(而不是try/catch/System.out.printStackTrace
【解决方案2】:

您可以查看Joda Money,它可能会为您的问题提供解决方案。注意:它仍然是 0.6 版本

【讨论】:

    【解决方案3】:

    java.util.Currency 中的getSymbol() 方法将帮助您获取货币符号

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 2016-04-22
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      相关资源
      最近更新 更多