msr msr - 2 years ago 75
Java Question

Simple division in Java - is this a bug or a feature?

I'm trying this simple calculation in a Java application:

System.out.println("b=" + (1 - 7/10));

Obviously I want
for the output but here's what I get

What?! Why does this happens?

If I make:

System.out.println("b=" + (1 - 0.7));

I get the right result which is

What's going wrong here?

Answer Source

You're using integer division.

Try 7.0/10 instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download