获得计算器的结果

时间:2018-01-30 09:57:21

标签: java android

enter image description here

private double calculate(String a,String b,String op){

    switch (op) {
        case "×":

            return Double.valueOf(a) * Double.valueOf(b);
        case "−":
            return Double.valueOf(a) - Double.valueOf(b);
        case "+":
            return Double.valueOf(a) + Double.valueOf(b);
        case "÷":
            try {
                return Double.valueOf(a) / Double.valueOf(b);
            } catch (Exception e) {
                Log.d("Calc", e.getMessage());
            }

        default:
            return -1;
    }
}

private boolean getResult() {
    if (currentOperator == "")
        return false;

    String[] operation = display.split(Pattern.quote(currentOperator));

    if (operation.length < 2)
        return false;
    result = String.valueOf(calculate(operation[0],operation[1],currentOperator));
    return true;
}

public void onclickEqual(View view) {
    btnSound=MediaPlayer.create(this,R.raw.waterdropclip);
    btnSound.start();
    if (display == "")
        return;
    if (!getResult())        //means the Result hasnt been calculated, Equal btn has no effect.
        return;
    operatioView.setText(display);
    ansView.setText((result));
    myHistory.add(operatioView.getText().toString());

}

获取字符串输入,以double计算并显示为String。为什么答案以指数形式(E)出现?我想要它&#34; long&#34;格式。 建议请。

1 个答案:

答案 0 :(得分:0)

试试这个

public String roundFiveDecimals(double d) 
 {
 DecimalFormat df = new DecimalFormat("#.#####");
 return df.format(d);
 }