Java 获取列中的总金额
我有下面的方法Java 获取列中的总金额,java,vaadin,Java,Vaadin,我有下面的方法 public void footerSet() { // Calculate total sum double totalSum = 0.0; // double totalMargin = 0.0; for (int i = 0; i < ic.size(); i++) { Item item = ic.getItem(ic.getIdByIndex(i)); try { do
public void footerSet() {
// Calculate total sum
double totalSum = 0.0;
// double totalMargin = 0.0;
for (int i = 0; i < ic.size(); i++) {
Item item = ic.getItem(ic.getIdByIndex(i));
try {
double valuetotal = Double.parseDouble((String) item
.getItemProperty("Amount").getValue());
totalSum += valuetotal;
System.out.println(">>>2222 : " + totalSum);
// System.out.println(">>>2222 : " + totalSum);
} catch (NumberFormatException e) {
// System.out.println("not a number");
e.printStackTrace();
}
}
ProductTableGlobal.setColumnFooter("Outlet", "Total");
ProductTableGlobal.setColumnFooter("Amount", String.valueOf(totalSum));
}
在模型中,我有下面的
//NumberFormat numberFormat = new DecimalFormat("#,###.00");
double totalAmounts = totalAmount;
passTable.getContainerProperty(UID, "Amount").setValue(
numberFormat.format((totalAmounts)));
当我试图把总数加起来时,我只得到第一列作为总数。见下面的日志
2222:460.0不是数字 请参见下面的堆栈跟踪 信息:已安装的AtmosphereInterceptor跟踪邮件大小拦截器使用|优先于|默认值 2222:460.0
首先,在循环内设置列页脚。下面的代码应该在循环外
ProductTableGlobal.setColumnFooter("Outlet", "Total");
ProductTableGlobal.setColumnFooter("Amount",String.valueOf(totalSum));
其次,您要强制转换为
String
,并使用toString
以及其中一个,在第二次迭代过程中,您得到的值不能解析为double,因此您没有得到一个正在打印的数字intcatch
block,因此要获得有关确切问题的更多信息,您应该使用e.printStackTrace()代码>在catch块中
2222 : 460.0
not a number
因此,从stacktrace中,我们可以看到您从Amount
属性中获得的值是
2,336.00
^
因此,由于,
中的字符串
的原因,无法将该值解析为双精度。简单的解决方案是使用,最好在解析之前使用null检查,这样可以避免空指针偏移
double valuetotal=0.0;
if(item!=null && item.getValue()!=null){
String tempString = item.getItemProperty("Amount")
.getValue()
.toString()
.replace(",","");
valuetotal=Double.parseDouble(tempString);
}
我已根据您的更改进行了编辑和更改,但问题仍然存在。我已添加堆栈跟踪。注:2336.00是需要添加的第二个值,依此类推。它现在运行良好。注意-我添加了ProductTableGlobal.setColumnFooter(“金额”,numberFormat.format(totalSum));
2222 : 460.0
not a number
2,336.00
^
double valuetotal=0.0;
if(item!=null && item.getValue()!=null){
String tempString = item.getItemProperty("Amount")
.getValue()
.toString()
.replace(",","");
valuetotal=Double.parseDouble(tempString);
}