Android计算器截断结果后不必要的小数位
我得到的结果始终采用十进制格式,就像如果我输入“1”,它会转换为“1.0”,最终结果也采用十进制格式。谁能告诉我如何始终以简单格式而不是十进制格式显示用户输入?
这与我之前的帖子有关:
private void handleEquals(int newOperator) {
if (hasChanged) {
switch (operator) {
case 1:
num = num + Double.parseDouble(txtCalc.getText().toString());
break;
case 2:
num = num - Double.parseDouble(txtCalc.getText().toString());
break;
case 3:
num = num * Double.parseDouble(txtCalc.getText().toString());
String strNum = null;
strNum = Double.toString(num);
if(strNum.contains("E")){
strNum = strNum.substring(0, 6) + strNum.substring(strNum.indexOf("E"));
}
Log.i("MULTIPLICATION","Checking Precision");
System.out.println("New StrNum is: " + strNum);
num = Double.valueOf(strNum);
break;
case 4:
num = num / Double.parseDouble(txtCalc.getText().toString());
break;
}
I am getting results always in decimal format like if I enter '1', it gets converted into '1.0' and final result also comes in decimal format. Can anyone tell me how to display user inputs always in simple format instead of decimal format?
This is related to my previous post :
private void handleEquals(int newOperator) {
if (hasChanged) {
switch (operator) {
case 1:
num = num + Double.parseDouble(txtCalc.getText().toString());
break;
case 2:
num = num - Double.parseDouble(txtCalc.getText().toString());
break;
case 3:
num = num * Double.parseDouble(txtCalc.getText().toString());
String strNum = null;
strNum = Double.toString(num);
if(strNum.contains("E")){
strNum = strNum.substring(0, 6) + strNum.substring(strNum.indexOf("E"));
}
Log.i("MULTIPLICATION","Checking Precision");
System.out.println("New StrNum is: " + strNum);
num = Double.valueOf(strNum);
break;
case 4:
num = num / Double.parseDouble(txtCalc.getText().toString());
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该测试您的数字是否是整数:
You should test if your number is an integer:
在每种情况下,在
break
之前添加以下行。更新答案:
或者
Add the following line before
break
in every case.Updated answer:
or