Decimal separator comma (',') with numberDecimal inputType in EditText

mseo picture mseo · Sep 29, 2010 · Viewed 112.6k times · Source

The inputType numberDecimal in EditText uses the dot '.' as decimal separator. In Europe it's common to use a comma ',' instead. Even though my locale is set as german the decimal separator is still the '.'

Is there a way to get the comma as decimal separator?

Answer

Martin picture Martin · Jun 8, 2011

A workaround (until Google fix this bug) is to use an EditText with android:inputType="numberDecimal" and android:digits="0123456789.,".

Then add a TextChangedListener to the EditText with the following afterTextChanged:

public void afterTextChanged(Editable s) {
    double doubleValue = 0;
    if (s != null) {
        try {
            doubleValue = Double.parseDouble(s.toString().replace(',', '.'));
        } catch (NumberFormatException e) {
            //Error
        }
    }
    //Do something with doubleValue
}