简体   繁体   English

使 EditText 只显示两位小数

[英]making EditText to show only two decimal places

i want to show only two decimal places in my edit text, ofc i wanna show currency in edit text but limiting its value to 2 digits after decimal.我想在我的编辑文本中只显示两个小数位,ofc 我想在编辑文本中显示货币,但将其值限制为小数点后 2 位。

I have seen some solutions using regular expression but i don't wanna do that.我已经看到一些使用正则表达式的解决方案,但我不想这样做。 I HAVE been told that java support some internal library functions that can do that.有人告诉我,java 支持一些可以做到这一点的内部库函数。 Any one can please give me hints or give me some efficient code for that.任何人都可以请给我提示或给我一些有效的代码。

Regards问候

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);

            }

You can simply use DecimalFormat您可以简单地使用DecimalFormat

DecimalFormat format = new DecimalFormat("##.##");
String formatted = format.format(22.123);
editText.setText(formatted);

You will get result in EditText as 22.12您将在EditText得到结果为22.12

Here is a solution that will limit the user while typing in the edit text.这是一个解决方案,可以在输入编辑文本时限制用户。

    InputFilter filter = new InputFilter() {
    final int maxDigitsBeforeDecimalPoint=2;
    final int maxDigitsAfterDecimalPoint=2;

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {
            StringBuilder builder = new StringBuilder(dest);
            builder.replace(dstart, dend, source
                    .subSequence(start, end).toString());
            if (!builder.toString().matches(
                    "(([1-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"

                    )) {
                if(source.length()==0)
                    return dest.subSequence(dstart, dend);
                return "";
            }

        return null;

    }
};

mEdittext.setFilters(new InputFilter[] { filter });

eg, 12.22 so only 2 digits before and two digits after the decimal ponit will be allowed to be entered.例如,12.22 所以只允许输入小数点前 2 位和后 2 位。

Input:输入:

 mBinding.etPriceUpdate.setText = getString(R.string.txt_rupees) + " " + "%.2f".format(firstNum!!.toDouble() * secondNum.toDouble())

Output:输出:

₹ 4567.54 4567.54 卢比

This will format the floating-point number 1.23456 up to 2 decimal places because we have used two after the decimal point in formatting instruction %.2f, f is for floating-point number, which includes both double and float data type in Java.这会将浮点数 1.23456 格式化为最多 2 个小数位,因为我们在格式化指令 %.2f 中使用了小数点后两位,f 用于浮点数,它包括 Java 中的 double 和 float 数据类型。 Don't try to use "d" for double here, because that is used to format integer and stands for decimal in formatting instruction.不要尝试在此处使用“d”表示双精度数,因为它用于格式化整数并在格式化指令中代表小数。 By the way, there is a catch here, format() method will also arbitrarily round the number.顺便说一下,这里有一个问题,format() 方法也会对数字进行任意舍入。 For example, if you want to format 1.99999 up-to 2 decimal places then it will return 2.0 rather then 1.99, as shown below.例如,如果您想将 1.99999 格式化为最多 2 个小数位,那么它将返回 2.0 而不是 1.99,如下所示。

You just need to assign setKeyListener() to your EditText .您只需要将setKeyListener()分配给您的EditText

myEditText.setKeyListener(DigitsKeyListener.getInstance(true,true));

Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.返回一个DigitsKeyListener ,它接受数字 0 到 9,加上减号(仅在开头)和/或小数点(每个字段仅一个)(如果指定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM