【问题标题】:Number format Exception on JSP Page with decimal values带有十进制值的 JSP 页面中的 Numberformatexception
【发布时间】:2015-03-31 04:47:41
【问题描述】:

我在我的 JSP 页面上使用下面的代码来显示逗号格式的成本,

   <%      java.util.Locale locale =  java.util.Locale.US;
  java.text.NumberFormat numberFormatter =       java.text.NumberFormat.getNumberInstance(locale);

    String specialChars = "..";

   Double d= Double.valueOf(cost);
   int cnvCost = d.intValue();

  %>


  <input id="test" class="form-control"  value="<%=numberFormatter.format(cnvCost) %>"   type="text" />  

但是对于十进制值,它会抛出 java.lang.NumberFormatException: For input string: "34876.98" 的异常

感谢您对解决此问题的任何帮助。

【问题讨论】:

  • 它不适合我。您确定异常没有在其他地方发生吗?
  • 在您的情况下,预期的输出是什么? 34,876??如果是这样,它工作得很好。
  • 是的,我期待输出 34,876

标签: java jsp number-formatting


【解决方案1】:
below code display result as you want.
<%
        java.util.Locale locale = java.util.Locale.US;
        java.text.NumberFormat numberFormatter = java.text.NumberFormat
                .getNumberInstance(locale);

        String specialChars = "..";
        String cost="34876.98";
        Double d = Double.valueOf(cost);
        int cnvCost = d.intValue();
    %>


    <input id="test" class="form-control"
        value="<%=numberFormatter.format(cnvCost)%>" type="text" /> 

【讨论】:

  • 大家好,感谢您的帮助,它现在正在工作。我在导致错误的代码之前使用了 Integer.parseInt(cost)。
猜你喜欢
  • 2019-04-18
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多