I'm making a calulator programme in java with a GUI on swing. My calculator after an operation, lets say multiplying throws out a double and even if there are no decimal places, the number looks for ex. like this: 2 * 2 = 4,0. I was wondering if there's a way to make the programme detect if it needs to show the decimal or not, so if the number is an integer it would show a whole number like: 2 * 2 = 4. I've try doing it this way:
JButton btnEquals = new JButton("=");
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SecondNum = Double.parseDouble(txtDisplay.getText());
if (operation == "+") {
txtDisplay.setText(String.valueOf(FirstNum + SecondNum));
if ((FirstNum + SecondNum) == Integer.parseInt(String.valueOf(FirstNum + SecondNum))){
txtDisplay.setText(String.valueOf(Integer.parseInt(String.valueOf(FirstNum + SecondNum))));
}
}
}
inside the firs "if" i've made a second one wher if the double result of First Number and second number is equal to the integer equivalent of this operation, the the display should show the integer form, sinc double is not need. This does not work, the programme crashes.