[英]DecimalFormat removes zero after dot
I want to format user's input and it's okay, but when I try to input zero after dot DecimalFormat
removes it. 我想格式化用户的输入,它没关系,但是当我尝试在点DecimalFormat
删除它之后输入零。 I use the following code: 我使用以下代码:
DecimalFormat df = new DecimalFormat("#,###.##");
Number n = df.parse(v);
amountEdit.setText(df.format(n));
Example Input/Output: 示例输入/输出:
9.0 -> 9. 9.0 - > 9。
9.9 -> 9.9 9.9 - > 9.9
9.90 -> 9.9 9.90 - > 9.9
It removes zeros! 它删除了零!
EDIT: 编辑:
I have EditText
with TextChangedListener
The idea is to format user's input like 999 999 999.99
(this is max value). 我有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;
}
});
My onCreate
contains: 我的onCreate
包含:
df = new DecimalFormat("#,###.00");
df.setDecimalSeparatorAlwaysShown(true);
dfnd = new DecimalFormat("#,###");
hasDot = false;
It removes zeros! 它删除了零!
Well yes, it would - you've specifically used .##
which means "only include digits if they're significant". 是的,它会 - 你专门使用.##
这意味着“只包括数字,如果它们是重要的”。 If you want to always have at least one decimal place, use 如果您想要始终至少有一个小数位,请使用
DecimalFormat df = new DecimalFormat("#,###.0#");
If you always want to have two decimal places, use: 如果您总想要两位小数,请使用:
DecimalFormat df = new DecimalFormat("#,###.00");
You should probably consider how you want 0.5 to be formatted, too. 您可能应该考虑如何格式化0.5。 Do you want "0.5" or ".5"? 你想要“0.5”还是“.5”?
'#' means include this digit if it matters and since 9,0 == 9
, the formatting removes it. '#'表示如果重要,请包括此数字,因为9,0 == 9
,格式化将删除它。
If you need a minimum of two digits after decimal then you should replace to format as: 如果您在十进制之后需要至少两位数,那么您应该将格式替换为:
DecimalFormat df = new DecimalFormat("#,###.00");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.