2

I am learning Java by debugging programs from various tutorials. At a moment I am fixing this calculator: http://www.dreamincode.net/forums/topic/321933-creating-a-calculator-using-jframe/
If you press "1/1" it displays "1.0" instead of "1". I am leaning towards

if (abs(result-round(result)) < 0.000000001){(int)(result)}

What would be an appropriate way to fix this glitch? Thank you in advance,

5
  • this may help stackoverflow.com/questions/14204905/… Commented Mar 18, 2016 at 2:33
  • (int)(result) is meaningless. Do you mean result=(int)(result);? Commented Mar 18, 2016 at 2:33
  • Yes, I mean result=(int)(result);. Is there a better way to achieve this? I am trying to learn good coding practices. Commented Mar 18, 2016 at 2:37
  • @StepanLenevich if you do (int)(result); you don't get 1.5 for 3/2 Commented Mar 18, 2016 at 2:38
  • That is why I am checking for if (abs(result-round(result)) < 0.000000001) Commented Mar 18, 2016 at 2:39

1 Answer 1

1

I think you are close to what you want, but this code is probably the more correct way to do it:

if (abs(result-round(result)) < 0.000000001){(int)Math.round(result);}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.