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.

public void equalsOnClick(View view)
{
    Integer result = null;
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");

    try {
        result = (int)engine.eval(workings);
    } catch (ScriptException e)
    {
        Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
    }

    if(result != null)
        resultsTV.setText(String.valueOf(result.intValue()));

}

MainActivity

public class MainActivity extends AppCompatActivity {

    TextView workingsTV;
    TextView resultsTV;

    String workings = "";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTextView();
    }

    private void initTextView()
    {
        workingsTV = (TextView)findViewById(R.id.workingsTextView);
        resultsTV = (TextView)findViewById(R.id.resultTextView);
    }

    private void setWorkings(String givenValue)
    {
        workings = workings + givenValue;
        workingsTV.setText(workings);
    }

    public void equalsOnClick(View view)
    {
        Double result = null;
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");

        try {
            result = (double)engine.eval(workings);
        } catch (ScriptException e)
        {
            Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
        }

        if(result != null)
            resultsTV.setText(String.valueOf(result.doubleValue()));
    }

    public void clearOnClick(View view)
    {
        workingsTV.setText("");
        workings = "";
        resultsTV.setText("");
        leftBracket = true;
    }
}

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:

public void equalsOnClick(View view) {
    Double result = null;
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");

    try {
        result = (Double)engine.eval(workings);
        if(result != null) {
            int intVal = (int)result.doubleValue();
            if(result == intVal) {// Check if it's value is equal to its integer part
                resultsTV.setText(String.valueOf(intVal));
            } else {
                resultsTV.setText(String.valueOf(result));
            }
        }
    } catch (ScriptException e) {
        Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
    }    
}

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