How to show result with Int on calculator?

孤人 提交于 2021-01-05 07:25:19

问题


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;
    }
}

回答1:


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.




回答2:


Use the modulo operator to check whether a double is an Integer (result %1 ==0) or Math.floor then check whether result changed or not.

If it is, you can use Integer.valueOf(result)

Integer has a built in toString method by the way.



来源:https://stackoverflow.com/questions/65479088/how-to-show-result-with-int-on-calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!