What I have:
I have an EditText which accepts decimal input and I setted the default value in the XML to 0.00
(if there is no input yet)
If the user press 1
, then the value change to 1
. If he needs to enter decimal value then he must press the dot .
on the keyboard.
What I want:
I want to auto format the EditText in a decimal format 0.00
, so if a user press 1
if becomes 0.01
instead of 1
(Like in the PAYPAL App). If he entered 100
, then the value in the EditText must be formatted as 1.00
.
I know this can be done by using TextWatcher
but I don't know how to achieve this.
End Result Comparison:
WHAT I HAVE
1 = 1
11 = 11
222 = 222
WHAT I NEED
1 = 0.01
11 = 0.11
222 = 2.22
UPDATE:
My actual TextWatcher looks like below just to set the default value to 0.00
if the user deletes all inputs (to prevent error on calculated functions).
private class EditTextListener implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (editText.getText().toString().equals("") || editText.length() < 1){
editText.setText("0.00");
editText.post(() -> editText.setSelection(editText.getText().length()));
} else {
BigDecimal amount = new BigDecimal(editText.getText().toString());
}
}
}
as you described you need to divide it by 100 with decimal points: