本文介绍了从众多的一个TextView显示删除小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于我的简单的计算器,我显示的结果在的TextView
,但它总是呈现出小数。如何删除他们?
这是我的code:
公共类MainActivity扩展活动实现OnClickListener {
的EditText etNum1;
的EditText etNum2;
按钮btnAdd;
按钮btnSub;
按钮btnMult;
按钮btnDiv;
TextView的tvResult;
字符串OPER =;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//找到的元素
etNum1 =(EditText上)findViewById(R.id.etNum1);
etNum2 =(EditText上)findViewById(R.id.etNum2);
btnAdd =(按钮)findViewById(R.id.btnAdd);
btnSub =(按钮)findViewById(R.id.btnSub);
btnMult =(按钮)findViewById(R.id.btnMult);
btnDiv =(按钮)findViewById(R.id.btnDiv);
tvResult =(TextView中)findViewById(R.id.tvResult);
//设置监听器
btnAdd.setOnClickListener((OnClickListener)本);
btnSub.setOnClickListener(本);
btnMult.setOnClickListener(本);
btnDiv.setOnClickListener(本);
}
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
双NUM1 = 0;
双NUM2 = 0;
双重结果= 0;
//检查字段为空
如果(TextUtils.isEmpty(etNum1.getText()的toString())
|| TextUtils.isEmpty(etNum2.getText()的toString())){
返回;
}
//读取的EditText并填写数字变量
NUM1 = Double.parseDouble(etNum1.getText()的toString());
NUM2 = Double.parseDouble(etNum2.getText()的toString());
//定义已被点击的按钮,并执行相应的操作
//写入操作OPER,稍后我们将用它来输出
开关(v.getId()){
案例R.id.btnAdd:
OPER =+;
结果= NUM1 + NUM2;
打破;
案例R.id.btnSub:
OPER = - ;
结果= NUM1 - NUM2;
打破;
案例R.id.btnMult:
OPER =*;
结果= NUM1 * NUM2;
打破;
案例R.id.btnDiv:
OPER =/;
结果= NUM1 / NUM2;
打破;
默认:
打破;
}
//形成输出线
tvResult.setText(NUM1 ++ OPER ++ NUM2 +=+结果);
}
}
解决方案
很多方法可以做到这一点,我使用的:
公共静态双圆(双精度值,整数位){
如果(地方℃下)
抛出新抛出:IllegalArgumentException();
BigDecimal的BD =新的BigDecimal(值);
BD = bd.setScale(地方,RoundingMode.HALF_UP);
返回bd.doubleValue();
}
然后调用:
圆(yourAnswer,2)
有关的BigDecimal:
http://developer.android.com/reference/java/math/BigDecimal.html
Efficient BigDecimal的圆上下两个小数
For my simple calculator, I am showing the result in a TextView
, but it is always showing decimals. How can I remove them ?
This is my code :
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);
}
}
解决方案
Many ways to do that, i use:
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();
}
And then call:
round(yourAnswer, 2)
For BigDecimal:
http://developer.android.com/reference/java/math/BigDecimal.html
Efficient BigDecimal round Up and down to two decimals
这篇关于从众多的一个TextView显示删除小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!