Unable to make backspace button function

121 views Asked by At

I am adding a backspace button in my calculator app and it is working fine also. The problem is, by default, my calculator is taking digits from decimal part i.e. initially it is 0.00, when I input 1 it becomes 0.01,when I input 2 it becomes 0.12 and so on and so forth. Now, when I press backspace it is deleting 2 and showing 0.01 but if I press 3 instead of showing 0.13 it shows 1.23. How to resolve this?

Code for the backspace button:-

private String addCurrency(String digits) {
        String string = ""; // Your currency

        enteredNumber = enteredNumber + digits;
        // Amount length greater than 2 means we need to add a decimal point
        if (enteredNumber.length() > 2) {
            String rupee = enteredNumber.substring(0,
                    enteredNumber.length() - 2); // Pound part
            String paise = enteredNumber.substring(enteredNumber.length() - 2); // Pence
                                                                                // part
            if (enteredNumber.contains(".")) {

            }

            string += rupee + "." + paise;
        } else if (enteredNumber.length() == 1) {
            string += "0.0" + enteredNumber;
            Log.d("TextWatcher", "length 1 " + string);
        } else if (enteredNumber.length() == 2) {
            string += "0." + enteredNumber;
            Log.d("TextWatcher", "length 2 " + string);
        }

        return string;
    }

WHERE:- enteredNumber is just a String type variable.

1

There are 1 answers

0
Darpan On

If you wish to show your numbers with two decimal points all the time, then why so much trouble? Use this -

public void getMyNumber(String yourInputNumber){
    Double result = Integer.ParseInt(yourInputNumber)/100;
    private static DecimalFormat REAL_FORMATTER = new DecimalFormat("0.##");
    textview1.setText(REAL_FORMATTER.format(result));
}