Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

            }
like image 399
Muhammad Umar Avatar asked Feb 17 '12 05:02

Muhammad Umar


Video Answer


1 Answers

You can simply use DecimalFormat

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

You will get result in EditText as 22.12

like image 63
Lalit Poptani Avatar answered Oct 20 '22 19:10

Lalit Poptani