2015-03-31 45 views
1

enter image description here我用我的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:对于输入字符串:“34876.98”

感谢帮助解决此问题。

+0

它不适合我。你确定这个异常没有发生在其他地方吗? – ramp 2015-03-31 05:42:09

+0

你的情况下的预期输出是什么? '34,876'?如果是的话,它运作良好。 – 2015-03-31 05:46:34

+0

是的,期待输出34,876 – Bipin 2015-03-31 06:26:30

回答

-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" /> 
+0

嗨,亲爱的,谢谢你的帮助,它现在正在工作。在导致错误的代码之前,我使用了Integer.parseInt(cost)。 – Bipin 2015-04-01 05:35:28