从TextView中显示的数字中删除小数

时间:2014-02-21 10:33:57

标签: android

对于我的简单计算器,我在TextView显示结果,但它始终显示小数。我该如何删除它们?

这是我的代码:

public class MainActivity extends Activity implements OnClickListener {

  EditText etNum1;
  EditText etNum2;

  Button btnAdd;
  Button btnSub;
  Button btnMult;
  Button btnDiv;

  TextView tvResult;

  String oper = "";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find the elements
    etNum1 = (EditText) findViewById(R.id.etNum1);
    etNum2 = (EditText) findViewById(R.id.etNum2);

    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnSub = (Button) findViewById(R.id.btnSub);
    btnMult = (Button) findViewById(R.id.btnMult);
    btnDiv = (Button) findViewById(R.id.btnDiv);

    tvResult = (TextView) findViewById(R.id.tvResult);

    // set a listener
    btnAdd.setOnClickListener((OnClickListener) this);
    btnSub.setOnClickListener(this);
    btnMult.setOnClickListener(this);
    btnDiv.setOnClickListener(this);

  }
      @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
     double  num1=0;
     double num2=0;
     double result=0;


    // check if the fields are empty
    if (TextUtils.isEmpty(etNum1.getText().toString())
        || TextUtils.isEmpty(etNum2.getText().toString())) {
     return;
    }

    // read EditText and fill variables with numbers

    num1 = Double.parseDouble(etNum1.getText().toString());
   num2 = Double.parseDouble(etNum2.getText().toString());



    // defines the button that has been clicked and performs the corresponding operation
    // write operation into oper, we will use it later for output
    switch (v.getId()) {
    case R.id.btnAdd:
      oper = "+";

     result = num1 + num2;

      break;
    case R.id.btnSub:
      oper = "-";
      result = num1 - num2;

      break;
    case R.id.btnMult:
      oper = "*";
      result = num1 * num2;

      break;
    case R.id.btnDiv:
      oper = "/";
     result = num1 / num2;

      break;
    default:
      break;
    }

    // form the output line




    tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);

  }
}

3 个答案:

答案 0 :(得分:1)

这是一种非数学方法:

一个简单的简单方法是,将double转换为string:

String text = String.valueOf(result);

现在你有了字符串中的结果。鉴于您的要求是您不需要小数,因此根据“。”拆分字符串。作为分隔符:

String str[] = text.split(".");

现在在str[0],您将只有数字部分。所以将它设置为文本视图:

tvResult.setText(num1 + " " + oper + " " + num2 + " = " + str[0]);

我确信这个工作正常。

答案 1 :(得分:0)

您可以使用DecimalFormat格式化结果:

DecimalFormat df = new DecimalFormat("0.##########");
tvResult.setText(num1 + " " + oper + " " + num2 + " = " + df.format(result));

这将打印最多10位小数。

答案 2 :(得分:0)

许多方法,我使用:

public static double round(double value, int places) {
        if (places < 0)
            throw new IllegalArgumentException();

        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }  

然后致电:

round(yourAnswer, 2)  

对于BigDecimal:
http://developer.android.com/reference/java/math/BigDecimal.html
Efficient BigDecimal round Up and down to two decimals