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

1. ## 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?

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

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

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

Java Code:
```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;
}```
I just found another rounding method that works fine. So, the problem is solved. Thanks.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•