DecimalFormat在点后删除零

时间:2017-10-30 08:49:52

标签: java android

我想格式化用户的输入并且没关系,但是当我尝试在点DecimalFormat删除它之后输入零时。 我使用以下代码:

 DecimalFormat df = new DecimalFormat("#,###.##");
 Number n = df.parse(v);
 amountEdit.setText(df.format(n)); 

示例输入/输出:

9.0 - > 9。

9.9 - > 9.9

9.90 - > 9.9

删除了零!

修改

EditText TextChangedListener 我们的想法是格式化用户的输入,如999 999 999.99(这是最大值)。

  amountEdit.addTextChangedListener(new TextWatcher() {
        @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 (data.document.isPaymentPossible) {

                if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())) || s.toString().contains(".")) {
                    hasDot = true;
                } else {
                    hasDot = false;
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            String string = s.toString().replaceAll("\\.", ",");

            if (string.equals(",") || string.equals(".")) {
                amountEdit.setText("");
                return;
            }

            amountEdit.removeTextChangedListener(this);

            payCustomAmount.setEnabled(amountEdit.getText().length() != 0);

            try {

                if (string.contains(",")) {
                    try {
                        String afterDot = string.split(",")[1];
                        if (afterDot.length() > 2) {
                            string = string.substring(0, string.length() - 1);
                            Number n = df.parse(string);
                            amountEdit.setText(df.format(n).replace(",", "."));
                            amountEdit.setSelection(amountEdit.getText().length());
                            amountEdit.addTextChangedListener(this);
                            showOverPaidText();
                            return;
                        }
                    } catch (Exception e) {
                        if (BuildConfig.DEBUG) {
                            SysUtils.logf("PaymentOptions input: " + s + "Exception: " + e);
                        }
                    }
                } else {
                    if (string.length() > 11) {
                        string = string.substring(0, string.length() - 1);
                        Number n = dfnd.parse(string);
                        amountEdit.setText(dfnd.format(n));
                        amountEdit.setSelection(amountEdit.getText().length());
                        showOverPaidText();
                        amountEdit.addTextChangedListener(this);
                        return;
                    }
                }

                int inilen, endlen;
                inilen = amountEdit.getText().length();

                String v = string.replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");

                int cp = amountEdit.getSelectionStart();
                if (hasDot) {
                    Number n = df.parse(v);
                    String ss = df.format(n).replace(",", ".");
                    amountEdit.setText(ss);
                } else {
                    Number n = dfnd.parse(v);
                    amountEdit.setText(dfnd.format(n));
                }
                endlen = amountEdit.getText().length();
                int sel = (cp + (endlen - inilen));
                if (sel > 0 && sel <= amountEdit.getText().length()) {
                    amountEdit.setSelection(sel);
                } else {
                    amountEdit.setSelection(amountEdit.getText().length() - 1);
                }
            } catch (NumberFormatException | ParseException e) {
                showOverPaidText();
                amountEdit.addTextChangedListener(this);

                if (BuildConfig.DEBUG) {
                    SysUtils.logf("PaymentOptions input: " + s + "Exception: " + e);
                }
                return;
            }
            showOverPaidText();
            amountEdit.addTextChangedListener(this);
            return;
        }
    });

我的onCreate包含:

df = new DecimalFormat("#,###.00");
    df.setDecimalSeparatorAlwaysShown(true);
    dfnd = new DecimalFormat("#,###");

    hasDot = false;

2 个答案:

答案 0 :(得分:11)

  

删除了零!

嗯,是的,它会 - 你专门使用.##这意味着&#34;只包括数字,如果它们是重要的&#34;。如果您希望始终至少有一个小数位,请使用

DecimalFormat df = new DecimalFormat("#,###.0#");

如果您总想要两位小数,请使用:

DecimalFormat df = new DecimalFormat("#,###.00");

您应该考虑如何格式化0.5。你想要&#34; 0.5&#34;或&#34; .5&#34;?

答案 1 :(得分:0)

'#'表示包含此数字(如果重要),自9,0 == 9起,格式化将其删除。

如果您在十进制后需要至少两位数字,则应将格式替换为:

DecimalFormat df = new DecimalFormat("#,###.00");