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.
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.