Java – decimalformat deletes zeros after a dot
•
Java
I want to format the user's input, it doesn't matter, but when I try to delete it by clicking decimal format, I enter zero
DecimalFormat df = new DecimalFormat("#,###.##"); Number n = df.parse(v); amountEdit.setText(df.format(n));
Example I / O:
9.0 – > 9.
9.9 – > 9.9
9.90 – > 9.9
It deleted zero!
Edit:
I have EditText and textchangedlistener. The idea is to format the user's input, such as 999 999.99 (this is the maximum value)
amountEdit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s,int start,int count,int after) { } @Override public void onTextChanged(CharSequence s,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; } });
My oncreate contains:
df = new DecimalFormat("#,###.00"); df.setDecimalSeparatorAlwaysShown(true); dfnd = new DecimalFormat("#,###"); hasDot = false;
Solution
Yes, it will - you use it exclusively## This means "include only numbers if they are important" If you want to always have at least one decimal place, use
DecimalFormat df = new DecimalFormat("#,###.0#");
If you always want two decimal places, use:
DecimalFormat df = new DecimalFormat("#,###.00");
You should probably consider how to format 0.5 Do you want "0.5" or ". 5"?
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码