如何正确显示BigDecimal作为字符串

时间:2013-07-15 09:40:29

标签: java android

我正在尝试学习java进入android编程 对于我的练习,我正在尝试编写一个带有基本算术运算和小数点按钮的计算器程序 我尝试使用原始数字类型(如int和double)来实现它,但如果我使用double作为变量类型,则2.111之类的数字显示为2.11099999,但除此之外它似乎工作正常(my question about this is given here
正如在我的另一个问题的答案中建议我修改我的程序使用BigDecimal数据类型而不是double,但现在当我点击1按钮时,尾数get显示为1.0,而不是像以前的程序中那样只有1曾经用过双 我怎样才能解决这个问题?

更新尾数的代码:

public void updateMantissa(){

    // mantissa_valueBD = BigDecimal.valueOf(mantissa_value);

    if(mantissa_value == BigDecimal.ZERO){
        mantissa_str = "0";
    }
    /*
    else if(ceil(mantissa_value) == mantissa_value){
        mantissa_str = String.valueOf(mantissa_valueBD);
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 ); 
    }

    else{
        mantissa_str = String.valueOf(mantissa_valueBD);
        mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
    }
    */

    // /*
    else if(mantissa_value.setScale(0, RoundingMode.CEILING)== mantissa_value){
        // mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_value.toString();
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 4 ); 
    }

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

完整代码(MainActivity类):

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;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class MainActivity extends Activity {
// private double mantissa_value = 0;
// private BigDecimal mantissa_value = new BigDecimal(0);
// private BigDecimal opr1BD, opr2BD, resBD, mantissa_valueBD;
/*
private double opr1 = 0;
private double opr2 = 0;
private double res = 0;
// */

BigDecimal opr1 = BigDecimal.ZERO;
BigDecimal opr2 = BigDecimal.ZERO;
BigDecimal res = BigDecimal.ZERO;
BigDecimal tempBD = BigDecimal.ZERO;
BigDecimal mantissa_value = BigDecimal.ZERO;

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 int numOfClicksAfterDecimal = 0;
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(){

    // mantissa_valueBD = BigDecimal.valueOf(mantissa_value);

    if(mantissa_value == BigDecimal.ZERO){
        mantissa_str = "0";
    }
    /*
    else if(ceil(mantissa_value) == mantissa_value){
        mantissa_str = String.valueOf(mantissa_valueBD);
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 ); 
    }

    else{
        mantissa_str = String.valueOf(mantissa_valueBD);
        mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
    }
    */

    // /*
    else if(mantissa_value.setScale(0, RoundingMode.CEILING)== mantissa_value){
        // mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_value.toString();
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 4 ); 
    }

    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){
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = mantissa_value.add(BigDecimal.valueOf(0.0));
        updateMantissa();
    }
    else{
        if(mantissa_value == BigDecimal.ZERO){
            if(numOfClicksAfterDecimal == 1){
                mantissa_str = mantissa_str + ".0";
            }
            else{
                mantissa_str = mantissa_str + "0";
            }
        }

        else if(mantissa_value.setScale(0, RoundingMode.CEILING)== mantissa_value){
            if(numOfClicksAfterDecimal == 1){
                mantissa_str = mantissa_str + ".0";
            }
            else{
                mantissa_str = mantissa_str + "0";
            }
        }

        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*0;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*0);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
        thisText.setText(mantissa_str);
    }
}

public void onClkBn1(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+1;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(1.0));
    }

    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*1;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*1);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn2(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+2;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(2.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*2;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*2);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn3(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+3;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(3.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*3;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*3);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn4(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+4;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(4.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*4;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*4);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn5(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+5;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(5.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*5;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*5);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn6(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+6;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(6.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*6;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*6);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn7(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+7;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(7.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*7;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*7);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn8(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+8;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(8.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*8;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*8);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn9(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+9;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(9.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*9;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*9);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

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

public void onClkBnCan(View view){
    mantissa_value = BigDecimal.ZERO;
    opr1 = BigDecimal.ZERO;
    opr2 = BigDecimal.ZERO;
    res = BigDecimal.ZERO;
    /*
    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;
        mantissa_value = BigDecimal.ZERO;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        // opr1 = opr1 + mantissa_value;
        opr1 = opr1.add(mantissa_value);
        mantissa_value = BigDecimal.ZERO;
    }
    oprLatest = OprTypes.PLUS;
}


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

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

public void onClkBnDiv(View view){
    if(oprDivClicked == false){
        oprDivClicked = true;
        opr1 = mantissa_value;
        mantissa_value = BigDecimal.ZERO;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        // opr1 = opr1 / mantissa_value;
        opr1 = opr1.divide(mantissa_value);
        mantissa_value = BigDecimal.ZERO;
    }
    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;
            mantissa_value = opr1.add(mantissa_value);
            break;
        case MINUS:
            // mantissa_value = opr1 - mantissa_value;
            mantissa_value = opr1.subtract(mantissa_value);
            break;
        case MULT:
            // mantissa_value = opr1 * mantissa_value;
            mantissa_value = opr1.multiply(mantissa_value);
            break;
        case DIV:
            // mantissa_value = opr1 / mantissa_value;
            mantissa_value = opr1.divide(mantissa_value);
            break;
        case NONE:
            break;
        }
    }
    oprLatest = OprTypes.NONE;
    oprPlusClicked = false;
    oprMinusClicked = false;
    oprMultClicked = false;
    oprDivClicked = false;
    updateMantissa();
}
 }

------编辑------
如果我这样做,

public static void main(String[] args){
    // double a;
    a = 2.111;
    System.out.println(a);
}

它显示的是2.111而不是2.1109999。这是为什么?

------编辑2 ------

我已对程序的逻辑进行了更改,因此它现在可以在字符串上执行与显示相关的任何操作,同时使用Double.parseDouble()函数将尾数字符串值转换为double以执行与之相关的任何操作。计算。如果有人想对其进行评论,则下面给出了更新的代码

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;
// import java.math.BigDecimal;
import java.text.NumberFormat;

public class MainActivity extends Activity {

     private double mantissa_value = 0;
    // private BigDecimal mantissa_value = new BigDecimal(0);
    // private BigDecimal opr1BD, opr2BD, resBD, mantissa_valueBD;
    // /*
    private double opr1 = 0;
    // */
    NumberFormat number =  NumberFormat.getNumberInstance();

    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 boolean resetMantissa = true;
    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(){
        // This is the version that should get called from the num pad buttons, 0 - 9
        updateMantissa(true);
    }
    public void updateMantissa(boolean frmNumPad){
        // this is the version that should get called from other buttons besides the num pad buttons
        if(frmNumPad != true){
            long mantissa_val_int_part = (long)mantissa_value;
            if(mantissa_value == (double)mantissa_val_int_part){
                mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 );
            }
            else if (mantissa_str.length() > 10){
                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){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }

        if(decimalClicked == false){
            if( !(Double.parseDouble(mantissa_str)==0.0) ) 
                mantissa_str = mantissa_str + "0";
        }
        else{
            mantissa_str = mantissa_str + "0";
        }
        TextView thisText = (TextView) findViewById(R.id.mantissa);
        thisText.setText(mantissa_str);
    }

    public void onClkBn1(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "1";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn2(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "2";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn3(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "3";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn4(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "4";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn5(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "5";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn6(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "6";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn7(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "7";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn8(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "8";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn9(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "9";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBnDot(View view){
        if(decimalClicked == false){
            decimalClicked = true;
            mantissa_str = mantissa_str + ".";
        }
    }

    public void onClkBnCan(View view){
        mantissa_value = 0;
        // /*
        opr1 = 0;
        // */
        oprLatest = OprTypes.NONE;
        oprPlusClicked = false;
        oprMinusClicked = false;
        oprMultClicked = false;
        oprDivClicked = false;
        decimalClicked = false;
        mantissa_value = 0;
        opr1 = 0;
        resetMantissa = true;
        updateMantissa(false);
    }


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


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

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

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


    public void onClkBnRes(View view){
        mantissa_value = Double.parseDouble(mantissa_str);
        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;
        decimalClicked = false;
        mantissa_str = Double.toString(mantissa_value);
        updateMantissa(false);
    }

    public void onClkBnOff(View view){
        finish();
    }

}

2 个答案:

答案 0 :(得分:1)

基本上它只显示Double方式。 在显示数字之前添加以下小代码应该会有所帮助。

BigDecimal chk=BigDecimal.valueOf(val);
BigDecimal chk3=chk.subtract(BigDecimal.valueOf(chk.intValue()));
if(chk3.compareTo(BigDecimal.ZERO)==0)
    System.out.print(BigDecimal.valueOf(chk.intValue()));
else
    System.out.print(chk);

chk是您要显示的值。只需添加chk3,它只是int的值。此代码为50.1打印50.1,为50打印50.00。希望这可以帮助。

- 编辑 -

我在你的代码中发现了另一个错误。你在做:

tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0)); 这是错误的,因为存储了最接近10.0的值,但你想要的确是10.0,那么你应该这样做:

tempBD = mantissa_value.multiply(BigDecimal.valueOf("10.0"));

如此修改了void onClkBn1(View view)的代码如下:

public void onClkBn1(View view){
if(decimalClicked == false){
    // mantissa_value = mantissa_value*10+1;
    tempBD = mantissa_value.multiply(BigDecimal.valueOf("10.0"));
    mantissa_value = tempBD.add(BigDecimal.valueOf("1.0"));
}

else{
    // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*1;
    tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*1);
    mantissa_value = mantissa_value.add(tempBD);
    numOfClicksAfterDecimal++;
}
updateMantissa();
}

我不确定您的使用方式tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*1);不应该是Math.pow(double, double)。所以请检查一下。

答案 1 :(得分:1)

通过阅读您链接的问题,不清楚您的问题是浮动精确问题还是浮动打印问题。原始问题中的人建议支持BigDecimal,但我觉得你最初的担忧与后者有关 - 即打印价值 - 这就是你仍然面临的问题。

使用String.valueOf()打印双值绝不是一个好主意。幕后发生的事情是进行转换以将浮点值转换为基于默认语言环境的字符串。您应该自行执行转换,而不是让系统进行转换。

这样做的方法是DecimalFormat类。示例代码:

double mantissa_value = 2.11099999;

DecimalFormat df = new DecimalFormat("0.###");
// Un-comment the line below to take English locale into account when converting
//df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));

// will print '2.11099999' this is what you get in your original case
System.out.println( String.valueOf(mantissa_value) ); 

// will print '2.111' or '2,111' depending on your locale. if you uncomment the line above for English locale it will print '2.111'
System.out.println( df.format(mantissa_value) ); 

上述十进制格式将在小数点后打印带有1,2或3位的double值。如果您需要变量精度,您可以在用户输入字符时将计数器保留在变量中,然后使用该计数器生成具有所需十进制数字的DecimalFormatBigDecimal案例可以采用相同的方法。我希望有帮助