我想在我的编辑文本中只显示两位小数,当然我想在编辑文本中显示货币,但将其值限制为小数点后 2 位。
我见过一些使用正则表达式的解决方案,但我不想那样做。有人告诉我,java 支持一些可以做到这一点的内部库函数。任何人都可以给我提示或给我一些有效的代码。
问候
amount.setRawInputType(Configuration.KEYBOARD_12KEY);
amount.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
DecimalFormat formatVal = new DecimalFormat("##.##");
String formatted = formatVal.format(s);
amount.setText(formatted);
}
最佳答案
您可以简单地使用 DecimalFormat
DecimalFormat format = new DecimalFormat("##.##");
String formatted = format.format(22.123);
editText.setText(formatted);
您将在 EditText
中得到结果为 22.12
关于java - 使 EditText 只显示两位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9323618/