Android计算器截断结果后不必要的小数位

发布于 2024-11-30 12:08:34 字数 869 浏览 2 评论 0原文

我得到的结果始终采用十进制格式,就像如果我输入“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-12-07 12:08:34

您应该测试您的数字是否是整数:

String result;

if (num==(int)num) {
    result = String.valueOf((int)num);
} else {
    result = String.valueOf(num);
}

// display result to user

You should test if your number is an integer:

String result;

if (num==(int)num) {
    result = String.valueOf((int)num);
} else {
    result = String.valueOf(num);
}

// display result to user
情话难免假 2024-12-07 12:08:34

在每种情况下,在 break 之前添加以下行。

int number = (int)num;

更新答案:

int num;
num = num + (int)Double.parseDouble(txtCalc.getText().toString());

或者

int num;
num = num + Integer.parseInt(txtCalc.getText().toString());

Add the following line before break in every case.

int number = (int)num;

Updated answer:

int num;
num = num + (int)Double.parseDouble(txtCalc.getText().toString());

or

int num;
num = num + Integer.parseInt(txtCalc.getText().toString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文