Want to format numbers greater than 20 digits in java?

1.3k views Asked by At

I want to format number with commas which are greater than 20 digits in Android. I used DecimalFormat class and TextWatcher on EditText but the problem is when I enter the number greater than 20 digits it shows 0 after 20 digits rather than number.
It works fine till 20 digits.
Any Help should be appreciated.Thanks in Advance

private 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;
        }
    }

}
1

There are 1 answers

1
Zaw Than oo On

Use BigDecimal. When/why should we use BigDecimal?

    String number = "12345678910111213141516000";
    BigDecimal bd = new BigDecimal(number);
    DecimalFormat formatter = new DecimalFormat("#,###,###,###.#####");
    System.out.println("You want it : " + formatter.format(bd));

    Number num = formatter.parse(number);
    System.out.println("You don't want it : " + formatter.format(num));

Output

You want it : 12,345,678,910,111,213,141,516,000
You don't want it : 12,345,678,910,111,213,000,000,000