Adariel Lzinski
Adariel Lzinski

Reputation: 1121

How to automatically add commas to large numbers in Android?

I have a TextView that holds a number value (which is constantly being updated). Is there a method I can use to automatically add a comma if the number increases?

This is my current code:

    String number = textView.getText().toString();
    double amount = Double.parseDouble(number);
    DecimalFormat formatter = new DecimalFormat("#,###");
    String formatted = formatter.format(amount);        
    textView.setText(formatted);

Upvotes: 7

Views: 15226

Answers (3)

Hiren Patel
Hiren Patel

Reputation: 52800

Takes an integer and returns a string formatted to the U.S. Locale

private String getFormatedAmount(int amount){
  return NumberFormat.getNumberInstance(Locale.US).format(amount);
}

In: 10 Out: 10

In: 100 Out: 100

In: 1000 Out: 1,000

In: 10000 Out: 10,000

In: 100000 Out: 100,000

In: 1000000 Out: 1,000,000

Upvotes: 23

Sieryuu
Sieryuu

Reputation: 1521

You can implements TextWatcher

public class NumberTextWatcher implements TextWatcher {

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

    private EditText et;

    public NumberTextWatcher(EditText et)
    {
        df = new DecimalFormat("#,###.##");
        df.setDecimalSeparatorAlwaysShown(true);
        dfnd = new DecimalFormat("#,###");
        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)
    {
        if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
        {
            hasFractionalPart = true;
        } else {
            hasFractionalPart = false;
        }
    }

}

To use it you can use

editText.addTextChangedListener(new NumberTextWatcher(editText));

Source : Roshka Dev Team

Upvotes: 8

Raghav Sood
Raghav Sood

Reputation: 82543

You can use DecimalFormat as it even supports locales (some places use . instead of ,)

String number = "1000500000.574";
double amount = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###.00");
String formatted = formatter.format(amount);

Upvotes: 19

Related Questions