# My double variable adds itself 0.032/33 and 0.064/65

• 08-26-2011, 09:34 PM
Josep_16
My double variable adds itself 0.032/33 and 0.064/65
Hello there. I'm having such an strange problem with a double variable which increases 0.032 and 0.064 by itself.
Each time I click on a JButton, the variable has to increase in 0.01, starting on 0. The thing is that after some clicks, it increases itself 0.032 or 0.064. It's ever at the same number of clicks; for example, when the number is 0.499 it goes to 0.564. Ever the same clicks. Of course, there's nothing in the (long)code that says it to add that numbers. What's happening?
Another problem that I have is that the same variable usually adds 0.011, or 0.0101, even if I say to it to add 0.010000000000000. How can I solve it?
• 08-26-2011, 09:47 PM
JosAH
Try to create a small example that compiles and shows the behaviour you just described; without it there's not much we can do.

kind regards,

Jos
• 08-26-2011, 10:53 PM
DarrylBurke
Quote:

Another problem that I have is that the same variable usually adds 0.011, or 0.0101, even if I say to it to add 0.010000000000000. How can I solve it?
BigDecimal (Java Platform SE 6)
What Every Computer Scientist Should Know About Floating-Point Arithmetic

db
• 08-27-2011, 12:55 AM
Josep_16
Ok I found where the problem came from. It was from the method that rounded the doubles. I don't know why it added 0.32 and 0.64 to the double until it arrived to 1.

```public double round(double number, int decimals)   {           double result;           BigDecimal res;           res = new BigDecimal(number).setScale(decimals, BigDecimal.ROUND_UP);           result = res.doubleValue();           return result;   }```