0

I have got EditText with double value.
The problem is that I want to limit numbers after dot to two like this: "#.##". Now user can input value like 10.9237474 but I want to let the user input only 10.92 after dot.
I tried InputFilter:

 enter_price.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});

but this function refers to all numbers not take into account dot symbol. Also I am using TextWatcher and tried sth like this:

public TextWatcher Sum_Edit = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            String x = s.toString();
            if(x.contains(".")){
              // here tried to set input filter also like above
            }

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            amountSet();

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

Additional problem is that I couldn't clear CharSequence s in TextWatcher after first time use. So when I clear EditText using "BackSpace" I can only put two numbers. In first time after run app I can input a lot of numbers. That's all.
After user inputs correct price, given value I am parsing from String to double and save in db. So I have sth like this:

String get_price_field = enter_price.getText().toString().trim();
Double price_field = Double.parseDouble(get_price_field);
//Then save double value to db.

I don't want to sth like this:

String get_price_field = enter_price.getText().toString().trim();
Double price_field = Double.parseDouble(get_price_field);
Format to String  like this: 
String formattedString = String.format("%.02f", price_field)
Then back to double
Double price_field2 = Double.parseDouble(formattedString);
Beacuse program executes with no sense the same work...

So how I can achieve limit to two double value after dot?

L23ko
  • 25
  • 4
  • you can check the below link: [https://stackoverflow.com/questions/5107901/better-way-to-format-currency-input-edittext](https://stackoverflow.com/questions/5107901/better-way-to-format-currency-input-edittext) – Majid Feyzi Apr 16 '21 at 18:55
  • Does this answer your question? [Better way to Format Currency Input editText?](https://stackoverflow.com/questions/5107901/better-way-to-format-currency-input-edittext) – javdromero Apr 16 '21 at 20:24

0 Answers0