BigDecimal Error

后端 未结 5 1785
走了就别回头了
走了就别回头了 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));
    }
    
    0 讨论(0)
  • 2021-01-13 02:08

    You need to have k as BigDecimal too, instead of double.

    0 讨论(0)
  • 2021-01-13 02:11

    Use BigDecimal.ROUND_HALF_UP (or .._DOWN or .._EVEN).

    Floating point calculations are inherently inaccurate and the small errors accumulate. That's why your end result is off by a small amount. If you always round up, a small positive error like 1.0000000001 becomes 1.01.

    Alternatively you can use BigDecimal also for the calculations. That way you won't have an error in the end result in the first place. Just remember to use the BigDecimal(String) constructor, or obtain the BigDecimal directly from the result set.

    0 讨论(0)
  • 2021-01-13 02:15

    I assume rst is a ResultSet. Make sure you are using getBigDecimal rather than Double.parseDouble(rst.getString(5)):

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

    And first of all: why aren't you adding these numbers in the database directly using appropriate SQL SUM query?

    0 讨论(0)
  • 2021-01-13 02:16

    As to your second question, double is a binary floating point number. This means that it is expressed as a sum of powers of two. Don't ever use those for calculating monetary values. (if that's what you're summing up there). BigDecimal uses decimal arithmetic, so this is more in line to what we use.

    Numbers such as 0.1 are infinite fractions in binary, in this case: 0.000110011... thus you cannot get a reliable and exact result from using double.

    0 讨论(0)
提交回复
热议问题