我在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:对于输入字符串:&#34; 34876.98&#34;
感谢您解决此问题的任何帮助。
答案 0 :(得分:-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" />