我正在尝试学习java以进入android编程
对于我的实践我正在尝试编写一个具有基本算术运算和小数点按钮的计算器程序
我尝试使用原始数字类型(例如int和double)来完成它,但是如果我使用作为变量类型的双倍,诸如 2.111 之类的数字显示为 2.11099999,但除此之外它似乎工作正常(我的问题在这里给出)
正如我的另一个问题的答案中所建议的那样,我修改了我的程序以使用 BigDecimal数据类型而不是双精度,但是现在当我单击 1 按钮时,尾数显示为 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();
}
}