How to prevent the user from entering a "." before any number?

2

I'm using TextWatcher to do other persistencies, but I caught on this part, I'm getting a value double and using it for several calculations so I can not get the value ".55" for example.

If I did not have to do calculations, Android would put 0 before the point automatically for me. Does anyone know how to solve this problem?

    
asked by anonymous 21.08.2015 / 20:34

3 answers

0

If you are doing calculations with double , then by default, numbers with . in the front are accepted and do not hamper calculation. But if you want to ban the use of "." before a number you can use regular expressions .

    
22.08.2015 / 01:18
0

Try to use BigDecimal for mathematical calculations. Besides working with precision, it has a good interaction with views.

Try this:

String strValue = myTextView.getText().toString();
BigDecimal decimalValue = new BigDecimal(valueStr.replace('.', ','));
    
24.08.2015 / 02:11
0

If you tell the data type in the editText xml can solve your problem?

<EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"/>
    
25.08.2015 / 17:20