ogulcan1912
ogulcan1912

Reputation: 25

In Java, convert the dot to comma in DecimalFormat replace method

public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        if( editText != null)
        {
            if(editText.getText().length() > 0){
                    String TL = null;
                    DecimalFormat formatter = new DecimalFormat("#,###");
                    String ss = "";
                    try{
                        ss = editText.getText().toString();
                        ss = ss.replace(".", "");
                        ss = ss.replace(",", "");
                        Long number = Long.parseLong(ss);
                        ss = formatter.format(number);
                    }catch(Exception e){
                        ss = e.toString();
                    }
                    changeText(editText,ss);
                }

            editText.setSelection(editText.length());
            }

    }

when the user enter the value like 50000 answer is 50.000 in editText. But, I dont want to dot. I want to comma like 50,000. How can I do it ?

Upvotes: 1

Views: 1905

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109567

See javadoc

formatter.setDecimalSeparator('.');
formatter.setGroupingSeparator(',');

Or more dangerous, as global

Locale.setDefaultLocale(Locale.US);

Upvotes: 2

Related Questions