BigDecimal Error

后端 未结 5 1827
走了就别回头了
走了就别回头了 2021-01-13 01:58

In Java, I have defined k as double k=0.0;

I am taking data from database and adding the same using while loop,

while(rst.         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-13 02:07

    You're still going via double. Stick to BigDecimal everywhere:

    BigDecimal k = BigDecimal.ZERO;
    while (rst.next()) {
        k = k.add(new BigDecimal(rst.getString(5));
    }
    

    Alternatively - and preferrably, if the field in the database is actually a decimal value:

    BigDecimal k = BigDecimal.ZERO;
    while (rst.next()) {
        k = k.add(rst.getBigDecimal(5));
    }
    

提交回复
热议问题