-2

I made a simple code whis keeps adding 0.1 to zero. this is the code:

static double num = 0;
    public static void main(String[] args) {
        num+=0.1;
        System.out.println(num);

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        main(null);
    }
}

This is what it outputs:

0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
1.0999999999999999
...

But I want the output to look like this:

0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
1.1
...

Any ideas why it does that or how to fix it please?

Tom Lenc
  • 765
  • 1
  • 17
  • 41
  • 1
    I find it very interesting how people just throw out downvotes around without at least leaving a constructive comment to explain why they think the answer is so bad that it deserves it. Suggested reading: [When downvotes are appropriate](http://stackoverflow.com/help/privileges/vote-down). – sstan Jun 13 '15 at 23:33

3 Answers3

0

Try:

    System.out.printf("%.1f" , num);

Instead of:

    System.out.println(num);

This will limit the decimal places to the right of the decimal one place. If you wanted three decimal places you could use %.3f

0

Aside from using String formatting as suggested already. Another thing to note is that floating point numbers are not entirely exact.

In Java, you'll want to use BigDecimal to accurately represent these types of values.

wateryan
  • 40
  • 9
-1

That's called rounding error. It's just a fact of life when it comes to double or float types. If you are curious, read up about it on wiki.

If you want exact, you can use the BigDecimal class. But it will crash if the number has an infinite number of digits, like 1/3. Though that won't be an issue in the snippet you gave.

twentylemon
  • 1,248
  • 9
  • 11
  • don't know how to use it.. – Tom Lenc Jun 13 '15 at 23:21
  • It's pretty simple, just read the docs. The other answer will probably work better for your simple program though. – twentylemon Jun 13 '15 at 23:22
  • 1
    @twentylemon: I've never heard of `BigDecimal` crashing because of values like `1/3`. Can you provide an example to back up your statement? – sstan Jun 13 '15 at 23:23
  • @TomLenc: That's what [the documentation](http://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html) is for. – T.J. Crowder Jun 13 '15 at 23:24
  • @sstan you can set the number of digits to round to. But from the docs: "If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown." – twentylemon Jun 13 '15 at 23:26
  • @twentylemon It's actually a game that I'm working on. And is not simple ;). – Tom Lenc Jun 13 '15 at 23:28
  • @twentylemon: I think you're misunderstanding the conditions where the exception can happen. Personally, I would edit out that part, because it sounds misleading. Unless you demonstrate what you mean by an example. – sstan Jun 13 '15 at 23:29
  • Maybe I should paste more from the docs. "In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown." – twentylemon Jun 13 '15 at 23:30
  • @twentylemon: An actual concrete example would be much better. Otherwise, it sounds like you're just quoting from the docs without truly understanding what it means yourself. In this case, I think the specific case mentioned by the docs is not applicable to OP's code, and may gave the wrong impression that `BigDecimal` is a ticking bomb if you use it. – sstan Jun 13 '15 at 23:35
  • `BigDecimal` works well for financial numbers, where you purposefully limit the arithmetic so you do not come to situations that would come to conflicting results: you are not going to split a dollar in exactly 3 but as 0.33+0.33+0.34. Someone is going to have the unfair advantage of 1 penny (0.01). Round+round+leftover. Taxes sometimes have a round down policy – YoYo Jun 14 '15 at 05:50