【问题标题】:Java.Locale IrelandJava.Locale 爱尔兰
【发布时间】:2016-06-08 09:19:52
【问题描述】:

我正在尝试在 Java 应用程序中格式化货币,所以我编写了 CurrencyFormatter。我正在尝试以 €123,456,789.00 的形式进行格式化,这在本地工作,但是当我部署到我的公司服务器时,变量显示为 $123,456,789.00

这是我的代码:

 import java.text.NumberFormat;
 import java.util.Locale;

 public class CurrencyFormatter {
     private static final Log log = Log.getLog(CurrencyFormatter.class);

     public static String getEuroFormat(Double num) {
     log.info("Formatting: " + num + " to euros");
     Locale ireland = new Locale("en","IE");
     NumberFormat irelandFormat = NumberFormat.getCurrencyInstance(ireland);
     return  irelandFormat.format(num);
   }
}

任何关于如何解决我的问题的建议将不胜感激。

【问题讨论】:

  • 我认为您的语言环境错误,请参阅stackoverflow.com/questions/4375410/…
  • 如果你在NumberFormat.getCurrencyInstance(Locale.UK); 中使用Locale.UK 就可以了?
  • 问题在于它的爱尔兰应用程序需要以欧元显示,而不是英国应用程序以英镑显示。

标签: java locale currency


【解决方案1】:

代码对我来说是正确的。我的猜测是服务器上缺少 en_IE 语言环境。您可以通过调用 NumberFormat.getAvailableLocales() 来检查支持哪些语言环境。

【讨论】:

    【解决方案2】:

    对于这个问题,我无法将此语言环境添加到服务器,因此我开发了以下解决方法来解决我的问题。

     import java.text.DecimalFormat;
     import java.text.NumberFormat;
     import java.util.Locale;
    
        public class CurrencyFormatter{
          public static String getEuroFormat(Double num){
                String pattern="\u20ac###,###.##";
                DecimalFormat euroFormatter = new DecimalFormat(pattern);
                String output = euroFormatter.format(num);
                return output;
          }
        }
    

    我没有尝试(但失败)使用 Locale,而是通过创建一个模式(上图)实现了一个格式化程序,并将这个模式传递给一个 DecimalFormat 对象。然后我用它来格式化我的输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      相关资源
      最近更新 更多