1

Hey guys i am building a java calculator and everything works fine except the fact that i am doing calculations with double numbers.

The problem is that when i want to do 0.3 + 0.5 = 0.8 works fine, but when i do 6 + 6 = 12.0

How can i fix this so when the result is .0 at the end it displays an integer?

My code is:

public void actionPerformed(ActionEvent e) {
                int j = 0;
                r2 = Double.parseDouble(textField.getText());
                if (option.equals("+")) {
                    result = r1 + r2;
                }
                if (option.equals("-")) {
                    result = r1 - r2;
                }
                if (option.equals("*")) {
                    result = r1 * r2;
                }
                if (option.equals("/")) {
                    result = r1 / r2;
                }
                textField.setText(result + " ");
            }
Kostas Drak
  • 3,222
  • 6
  • 28
  • 60
  • 1
    try http://stackoverflow.com/questions/14204905/java-how-to-remove-trailing-zeros-from-a-double – kajacx Apr 12 '14 at 18:32

3 Answers3

3

Use a DecimalFormat object (see the documentation at http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html).

Example:

DecimalFormat df = new DecimalFormat("############.#");
System.out.println(df.format(result));
Hidde
  • 11,493
  • 8
  • 43
  • 68
  • Does't this transform `12.34` into just `12.3` ? – kajacx Apr 12 '14 at 18:40
  • Exactly. You can add as many `#` characters you want, to get the precision you want. Any 'zero endings' will automatically be removed from the string. – Hidde Apr 12 '14 at 18:48
2

You can check if your result is int and if it is then then set its integer value to result like this.

public void actionPerformed(ActionEvent e) {
            int j = 0;
            r2 = Double.parseDouble(textField.getText());
            if (option.equals("+")) {
                result = r1 + r2;
            }
            if (option.equals("-")) {
                result = r1 - r2;
            }
            if (option.equals("*")) {
                result = r1 * r2;
            }
            if (option.equals("/")) {
                result = r1 / r2;
            }
            if ((result == Math.floor(result )) && !Double.isInfinite(result )) {
                 textField.setText((int)(result) + " ");
            } else textField.setText(result + " ");
        }
er_suthar
  • 319
  • 1
  • 8
1

You can just check if result has a whole number in it, like this:

if((int) result == result) {
       textField.setText((int)result + " ");
} else {
       textField.setText(result + " ");
}

EDIT

As @Hovercraft Full Of Eels said, you could put more thought into deciding, what is a whole number and what is not, especialy for case when result is something like 5.99999997 and simple casting to int will make 5 from such number.

final double epsilon = 1e-6;
if(Math.abs(Math.rint(myVal) - myVal) < epsilon) {
    textField.setText(Math.round(myVal) + "");
} else {
    textField.setText(String.format("%f", myVal));
}
kajacx
  • 12,361
  • 5
  • 43
  • 70
  • I think this is exactly what the OP wants. But did you verify that the comparison is double comparison instead of integer? The net effect should be `if((double)((int) result) == result)`. – Bhesh Gurung Apr 12 '14 at 18:39
  • 2
    You're much better off checking if the numbers are *very* close together using a small epsilon value in this test, since this type of equality test often fails with doubles. Please look [here](http://mindprod.com/jgloss/floatingpoint.html). – Hovercraft Full Of Eels Apr 12 '14 at 18:40
  • @BheshGurung Yes, you could add cast back to double to be sure, but Java compiler will add that cast for you. – kajacx Apr 12 '14 at 18:42
  • Apart from the casting and all the float problems that introduces, *don't convert primitives to strings using string concatenation* (`+""`), the `String.valueOf` methods are there just for that. – Darkhogg Apr 13 '14 at 12:44