简体   繁体   中英

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 ?

See javadoc

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

Or more dangerous, as global

Locale.setDefaultLocale(Locale.US);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM