1

我正在尝试为android制作一个基本的计算器程序,但我对android和java都是新手
但我相信这可能更像是一个java问题
界面有0到9的按钮,小数点,等于符号,+, -、*、/ 和 Cancel(重置所有变量)
尾数存储在一个双精度变量中,我用String.valueOf(mantissa_value)它来将其转换为字符串以显示在 TextView 控件
上当我输入 2.331 时,尾数显示 2.33099999(10 characters 是存储尾数文本的 String 变量的限制),但如果我再次按 1 ,它会显示 2.3311。为什么会发生这种情况,我该如何解决?

相关代码(可能):

public void onClkBn1(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+1;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*1;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void updateMantissa(){
    if(mantissa_value == 0){
        mantissa_str = "0";
    }

    else if(ceil(mantissa_value) == mantissa_value){
        mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 ); 
    }

    else{
        mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
    }
    TextView thisText = (TextView) findViewById(R.id.mantissa);
    thisText.setText(mantissa_str);
}

完整代码:

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import static java.lang.Math.pow;
import static java.lang.Math.ceil;

public class MainActivity extends Activity {
private double mantissa_value = 0;
private double opr1 = 0;
private double opr2 = 0;
private double res = 0;
private enum OprTypes {NONE, PLUS, MINUS, MULT, DIV}
OprTypes oprLatest = OprTypes.NONE;
private boolean oprPlusClicked = false;
private boolean oprMinusClicked = false;
private boolean oprMultClicked = false;
private boolean oprDivClicked = false;
private boolean decimalClicked = false;
private double numOfClicksAfterDecimal = 1;
private String mantissa_str;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void updateMantissa(){
    if(mantissa_value == 0){
        mantissa_str = "0";
    }

    else if(ceil(mantissa_value) == mantissa_value){
        mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 ); 
    }

    else{
        mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
    }
    TextView thisText = (TextView) findViewById(R.id.mantissa);
    thisText.setText(mantissa_str);
}

public void onClkBn0(View view){
    TextView thisText = (TextView) findViewById(R.id.mantissa);
    if(decimalClicked == false){
        mantissa_value = mantissa_value*10+0;
        updateMantissa();
    }
    else{
        if(mantissa_value == 0){
            if(numOfClicksAfterDecimal == 1){
                mantissa_str = mantissa_str + ".0";
            }
            else{
                mantissa_str = mantissa_str + "0";
            }
        }

        else if(ceil(mantissa_value) == mantissa_value){
            if(numOfClicksAfterDecimal == 1){
                mantissa_str = mantissa_str + ".0";
            }
            else{
                mantissa_str = mantissa_str + "0";
            }
        }

        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*0;
        numOfClicksAfterDecimal++;
        thisText.setText(mantissa_str);
    }
}

public void onClkBn1(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+1;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*1;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn2(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+2;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*2;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn3(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+3;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*3;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn4(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+4;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*4;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn5(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+5;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*5;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn6(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+6;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*6;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn7(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+7;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*7;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn8(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+8;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*8;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn9(View view){
    if(decimalClicked == false)
        mantissa_value = mantissa_value*10+9;
    else{
        mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*9;
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBnDot(View view){
    decimalClicked = true;
}

public void onClkBnCan(View view){
    mantissa_value = 0;
    opr1 = 0;
    opr2 = 0;
    res = 0;
    oprLatest = OprTypes.NONE;
    oprPlusClicked = false;
    oprMinusClicked = false;
    oprMultClicked = false;
    oprDivClicked = false;
    decimalClicked = false;
    numOfClicksAfterDecimal = 1;
}


public void onClkBnPlus(View view){
    if(oprPlusClicked == false){
        oprPlusClicked = true;
        opr1 = mantissa_value;
        mantissa_value = 0;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        opr1 = opr1 + mantissa_value;
        mantissa_value = 0;
    }
    oprLatest = OprTypes.PLUS;
}


public void onClkBnMinus(View view){
    if(oprMinusClicked == false){
        oprMinusClicked = true;
        opr1 = mantissa_value;
        mantissa_value = 0;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        opr1 = opr1 - mantissa_value;
        mantissa_value = 0;
    }
    oprLatest = OprTypes.MINUS;
}

public void onClkBnMult(View view){
    if(oprMultClicked == false){
        oprMultClicked = true;
        opr1 = mantissa_value;
        mantissa_value = 0;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        opr1 = opr1 * mantissa_value;
        mantissa_value = 0;
    }
    oprLatest = OprTypes.MULT;
}

public void onClkBnDiv(View view){
    if(oprDivClicked == false){
        oprDivClicked = true;
        opr1 = mantissa_value;
        mantissa_value = 0;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        opr1 = opr1 / mantissa_value;
        mantissa_value = 0;
    }
    oprLatest = OprTypes.DIV;
}


public void onClkBnRes(View view){
    if(oprPlusClicked == true || oprMinusClicked == true || oprMultClicked == true || oprDivClicked == true){
        switch(oprLatest){
        case PLUS:
            mantissa_value = opr1 + mantissa_value;
            break;
        case MINUS:
            mantissa_value = opr1 - mantissa_value;
            break;
        case MULT:
            mantissa_value = opr1 * mantissa_value;
            break;
        case DIV:
            mantissa_value = opr1 / mantissa_value;
            break;
        case NONE:
            break;
        }
    }
    oprLatest = OprTypes.NONE;
    oprPlusClicked = false;
    oprMinusClicked = false;
    oprMultClicked = false;
    oprDivClicked = false;
    updateMantissa();
}
}
4

1 回答 1

1

A Double in Java is not exact. It can be fixed by using BigDecimal

See How to resolve a Java Rounding Double issue

于 2013-07-12T08:27:43.653 回答