0

I am using this TextWatcher for .addtextOnChangeListener, the output of the string is ex: "123,456,77" i want it to be "123.456,77" if i use the replace method on "et" in the "afterTextChanged" method, the number isn't even formatting. With the code below the listener works and everything, i just don't know how to replace the "," with "." until the decimals.

If you think to just change the pattern ("###,##0,00") it doesn't work

This is the TextWatcher I have for the EditText

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Locale;

public class NumberTextWatcher implements TextWatcher {

    private DecimalFormat df;
    private DecimalFormat dfnd;
    private boolean hasFractionalPart;

    private EditText et;

    public NumberTextWatcher(EditText et)
    {
        df = new DecimalFormat("###,##0,00");
        df.setDecimalSeparatorAlwaysShown(true);
        dfnd = new DecimalFormat("###,##0,00");
        this.et = et;
        hasFractionalPart = false;
    }

    @SuppressWarnings("unused")
    private static final String TAG = "NumberTextWatcher";

    @Override
    public void afterTextChanged(Editable s)
    {


        et.removeTextChangedListener(this);

        try {
            int inilen, endlen;
            inilen = et.getText().length();

            String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
            Number n = df.parse(v);
            int cp = et.getSelectionStart();
            if (hasFractionalPart) {
                et.setText(df.format(n));
            } else {
                et.setText(dfnd.format(n));
            }
            endlen = et.getText().length();
            int sel = (cp + (endlen - inilen));
            if (sel > 0 && sel <= et.getText().length()) {
                et.setSelection(sel);
            } else {
                // place cursor at the end?
                et.setSelection(et.getText().length() - 1);
            }

            
        } catch (NumberFormatException nfe) {
            // do nothing?
        } catch (ParseException e) {
            // do nothing?
        }



        et.addTextChangedListener(this);



    }

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    int index = s.toString().indexOf(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()));
    int trailingZeroCount = 0;
if (index > -1)
    {
        for (index++; index < s.length(); index++) {
            if (s.charAt(index) == '0')
                trailingZeroCount++;
            else {
                trailingZeroCount = 0;
            }
        }

        hasFractionalPart = true;
    } else {
        hasFractionalPart = false;
    }

    {
        if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
        {
            hasFractionalPart = true;
        } else {
            hasFractionalPart = false;
        }
    }

}
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
ivan14
  • 11
  • 2
  • Possibly related: https://stackoverflow.com/a/16686403 – Pshemo Jun 29 '22 at 11:16
  • BTW `if(condition) {variable = true;} else {variable=false;}` can be written as `variable = condition;`. – Pshemo Jun 29 '22 at 11:18
  • So if the user types in `123,456,776`, then the output should be `123.456.776`? You should review your current code. Both `DecimalFormat` have the same pattern. Also, you don't necessarily correctly identify if a number has decimals. Overall it would probably better to define a format you expect, lets say thousand separator as `,` and decimal as `.` (or vice versa). Then you can parse the input with the correct format. Then you have a number you can in turn format with your desired output format – XtremeBaumer Jun 29 '22 at 11:47
  • The user input is '123456776' and the formatter splits it with , – ivan14 Jun 29 '22 at 12:16
  • i need the thoused separator to be '.' and the decimal separator to be ',' – ivan14 Jun 29 '22 at 12:17

1 Answers1

0

Use this:

DecimalFormat decimalFormat=new DecimalFormat();
DecimalFormatSymbols decimalFormatSymbols=DecimalFormatSymbols.getInstance();
decimalFormatSymbols.setDecimalSeparator(',');
decimalFormatSymbols.setGroupingSeparator('.');
decimalFormat.setDecimalFormatSymbols(decimalFormatSymbols);
String formattedNumber=decimalFormat.format(123456.78);// prints 123.456,78
mostafa3dmax
  • 997
  • 7
  • 18