从字符串中删除最后一位数字

时间:2019-12-30 17:53:39

标签: java string substring calculator

我正在创建一个Java计算器(只有按钮-如T9键),并且我想修复“取消”按钮。

当我单击它时,我想从仅包含数字(或“。”的字符串)中取消最后一位数字,因为您也可以添加“。”来写十进制数字。 有一个问题:如果用户输入一个整数(34)并将其转换为双精度数,则它将变为34.0,如果我取消了最后一位,它将取消0。

我也尝试过使用String.substring(start,end),但是它不起作用... 您对取消句柄有什么建议吗?谢谢!

我正在为Android XML应用程序工作,这是取消最后一位数字的功能。

/* tv -> TextView
- tvResult contains what the user writes and the result when he clicks "=", - tvCalc contains the calc that the user is entering.
For example, if the user writes with the 0-9 buttons and the operator of the operations, in tvCalc there will be "34+50=", and in tvResult "84" */

public void handleCancel(View v) {
    //tv -> TextView, tvResult contains what the user writes and the result when he clicks "=", tvCalc contains the calc that the user is entering: for example if user writes with the 0-9 buttons and the operator of the operations, in tvCalc there will be "34+50=", and in tvResult "84"

    if (tvResult.length() == 0) {
        errorToast = Toast.makeText(MainActivity.this, "Error! There's nothing to cancel!", Toast.LENGTH_LONG);
        errorToast.show();
    } else {
        tvResult.setText(tvResult.toString().substring(0, tvResult.toString().length() - 1))

        if (tvCalc.toString().contains("=")) {
            tvCalc.setText(tvResult.toString());
            operand1 = tvResult.toString();
        } else {
            tvCalc.setText(tvCalc.toString().substring(0, tvCalc.toString().length() - 1));
            if (operator == "") operand1 = tvResult.toString();
            else {
                operand2 = tvResult.toString();
                try {
                    int conv = Integer.parseInt(tvCalcolo.toString());
                    operazione = "";
                } catch(Exception e) {}
            }
        }
    }
}

添加用户选择的功能:

public void userChoice(View v)
{
    Button clicked=(Button)findViewById(v.getId());
    String choice=clicked.getText().toString();
    try
    {
        int number=Integer.parseInt(choice);
        if (operator=="") 
        {
            operand1+=choice;
            if (tvCalc.toString().contains("=")) tvCalc.setText(operand1);
        } else operand2+=choice;
    } catch (Exception e)
    {
        if (choice.equals('.'))
        {
            if (operator=="") operand1+=choice;
            else operand2+=choice;
        } else 
        {
            if (operand2!="")
            {
                handleEqual(v);
                operand1=tvResult.toString();
            }
            operator=choice;
            tvCalc.append(operator);
            tvResult.setText("");
            return;
        }
    }

    tvCalc.append(choice);
    tvResult.append(choice);
}

2 个答案:

答案 0 :(得分:0)

看起来您至少可以通过两种方式满足您的要求:

  1. 要删除的模式搜索符号(类似(.0 | \ d | + |-| * | /))-不推荐
  2. 在添加新数字/操作之前记住最后一个输入字符串。 F.e.在按钮上。按下该按钮,您会记得您当前输入到计算器的变量,如果您需要取消,只需将当前输入行设置为该变量即可。

希望我的建议对您有帮助。

答案 1 :(得分:0)

看看这是否对您有帮助:

// Let's make it a double so we have something to work with.
double d = Double.parseDouble(tvResult.toString());

String newText;
if (d % 1 == 0) { // See if the number is a whole number
    int i = (int) d; // If it is, cast it to an int to get rid of the decimal
    newText = String.valueOf(i); // Parse it to a string so we can clip off the end
    newText = newText.substring(0, newText.length() - 1); // Clip off the end
} else { 
    // If it's not a whole number, just parse it to a string.
    newText = String.valueOf(d);
    newText = newText.substring(0, newText.length() - 1); // Clip off the end
    if (newText.endsWith(".")) { 
        // If the number we clipped off was a tenth, clip off the decimal
        newText = newText.substring(0, newText.length() - 1);
    }
}

tvResult.setText(newText);