java.math.BigDecimal cannot be cast to java.lang.String?

不打扰是莪最后的温柔 提交于 2019-12-24 11:03:14

问题


I am trying to add row values of datatype(money) in which zeros after decimal are automatically formed in Jtable like these 55.0000 28.0000 60.0000 20.0000 50.0000

on runtime log showing this error

java.math.BigDecimal cannot be cast to java.lang.String

Here is the code for rows addition from Jtable

        double total = 0;
        for(int i = 0; i<table.getRowCount(); i++){
            double amount = Double.parseDouble((String) table.getValueAt(1, 6) );
            total+=amount;
        }
        totalSum.setText(String.valueOf(total));

*

Is there any way to add float type values??

    *

回答1:


Try something like this:

BigDecimal total = BigDecimal.ZERO;
for (int i = 0; i < table.getRowCount(); ++i) {
    final BigDecimal amount = (BigDecimal)table.getValueAt(i, 6);
    total = total.add(amount);
}
totalSum.setText(total.toString());



回答2:


Change

double amount = Double.parseDouble((String) table.getValueAt(1, 6) );

to

double amount = table.getValueAt(1, 6).doubleValue();

There's no need to convert a BigDecimal to String just to make a double.



来源:https://stackoverflow.com/questions/45686654/java-math-bigdecimal-cannot-be-cast-to-java-lang-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!