Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have Android automatically add commas in large numbers?

Tags:

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);
like image 573
Adariel Lzinski Avatar asked Aug 28 '13 02:08

Adariel Lzinski


3 Answers

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

like image 139
Hiren Patel Avatar answered Nov 13 '22 10:11

Hiren Patel


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);
like image 19
Raghav Sood Avatar answered Nov 13 '22 10:11

Raghav Sood


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

like image 8
Sieryuu Avatar answered Nov 13 '22 10:11

Sieryuu