Skip to content
Advertisement

How to show result with Int on calculator?

I am making the calculator app. It shows double at the result even when I don’t use double. Example) 1+1 = 2.0

But I want like 1+1= 2

Of course, I want to keep double when there is double like 1.2+1.3= 2.5

How should I have to edit?

I tried to edit like this, but there is an error.

JavaScript

MainActivity

JavaScript

Advertisement

Answer

It is happening because you have declared result of type, Double. Therefore, until you cast it’s doubleValue() into an int and set the same to resultsTV, its double value will be set there.

Change your method definition as follows:

JavaScript

Note that I have also moved resultsTV.setText inside the try-catch block so that it gets executed only when result = (Double)engine.eval(workings) does not throw an exception.

Advertisement