gpt4 book ai didi

java - DecimalFormat java允许在点后输入0

转载 作者:行者123 更新时间:2023-12-02 12:56:10 27 4
gpt4 key购买 nike

我有一个 DecimalFormat ,其模式如下 "#,###.###"但是,当我想输入例如 50.05 时,它不允许这样做,因为每次文本更改以及调用

时,我都会将字符串解析为数字
df.parse("50.0");

它返回50.并剪切0

有什么办法可以输入像 50.05 这样的数字吗?

我的类(class)的完整代码如下所示。抱歉,如果太冗长:

private class NumberTextWatcher implements TextWatcher {
@SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";

private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;

private EditText et;

NumberTextWatcher(EditText et) {
df = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
df.applyLocalizedPattern(hasThousandSeparator ? "#,###.###" : "####.###");
df.setDecimalSeparatorAlwaysShown(true);
df.setMaximumFractionDigits(maxDecimal);
dfnd = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
dfnd.applyLocalizedPattern(hasThousandSeparator ? "#,###" : "####");
dfnd.setMaximumFractionDigits(maxDecimal);
this.et = et;
hasFractionalPart = false;
}

@Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);

try {
int inilen, endlen;
inilen = et.getText().length();

String sign = hasSign ? s.toString().startsWith("-") ? "-" : "+" : null;
String sStr = hasSignAttached(s) ? s.toString().substring(1) : s.toString();

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

if(v.length() == 0){
return;
}

if(hasFractionalPart){
String[] vSplit = v.split("\\.");

String intStr = vSplit[0];
String decStr = vSplit.length > 1 ? vSplit[1] : "";

if(intStr.length() > maxNumbers)
intStr = intStr.substring(0, maxNumbers);

if(decStr.length() > maxDecimal)
decStr = decStr.substring(0, maxDecimal);

v = String.format("%s.%s", intStr, decStr);
} else {
if(v.length() > maxNumbers)
v = v.substring(0, maxNumbers);
}


Number n = df.parse(v);
int cp = et.getSelectionStart();

String formatted;

if (hasFractionalPart) {
formatted = df.format(n);
} else {
formatted = dfnd.format(n);
}

et.setText(hasSign ? String.format("%s%s", sign, formatted) : formatted);

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 (Exception e) {
ExceptionTracker.trackException(e);
} finally {
et.addTextChangedListener(this);
}
}

private boolean hasSignAttached(Editable s){
return s.toString().startsWith("+") || s.toString().startsWith("-");
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(hasSign && start == 1 && count == 1){
et.setText("");
}
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
hasFractionalPart = s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()));
}
}

最佳答案

此代码解决了问题:

import java.text.DecimalFormat;

public class DecimalFormatExample {

public static void main(String[] args) {

DecimalFormat dformat = new DecimalFormat("#,###.###");
dformat.setMinimumFractionDigits(1);

String someNumber = "50.05";
Double someNumberDouble = Double.valueOf(someNumber);
System.out.println(dformat.format(someNumberDouble)); // 50.05

someNumber = "50.0";
someNumberDouble = Double.valueOf(someNumber);
System.out.println(dformat.format(someNumberDouble)); // 50.0

}
}

关于java - DecimalFormat java允许在点后输入0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44434503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com