Search code examples
androidfloating-pointnumberformatexceptiontextwatcher

NumberFormatException on DecimalFormat Float


after many attempts I managed to create a pattern for Float , I managed to do it by return ActivityResult and set it in the corresponding field . Now I have a problem with this TextWatcher that does not accept the pattern of Float , can anyone help me out ?

This is the TextWather

   private abstract class TextChangedListener implements TextWatcher
{

    public abstract void numberEntered(Float number);

    @Override
    public void afterTextChanged(Editable s)
    {
        String text = s.toString();
        try
        {
            Float parsedFloat = Float.valueOf(text);
            numberEntered(parsedFloat);
        } catch (NumberFormatException e)
        {
            Log.w(getPackageName(), "Non si puo' parsare '" + text + "' col numero", e);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }

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

This is the Float pattern

DecimalFormat dec = new DecimalFormat("###,##0.00", DecimalFormatSymbols.getInstance(Locale.ITALIAN));

Solution

  • Thanks to all! I resolve my problem with this method!

        private void decimalFormatWithSymbol() {
        dec = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.ITALIAN));
        dfs = new DecimalFormatSymbols();
        dfs.setDecimalSeparator('.');
        dec.setDecimalFormatSymbols(dfs);
        }