-2

I'm working on a program which contains EditText. EditText only accept DecimalNumber .

But in my case :- this EditText accepts ".3232"

what I need:- Output should be "0.3232"

How should I achieve this? I have used

android:inputType="numberDecimal|number"

Help Needed

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68

2 Answers2

1

But in my case:- this EditText accept ".3232"

its already mean that .3232 == 0.3232

you if you want than you need to do it programmetically

String outPut= String.format("%.04f", yourValueFloat .3232);
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
Pramod mishra
  • 615
  • 6
  • 16
0

Add TextWatcher and add this in afterTextChanged

@Override
    public void afterTextChanged(Editable s) {            
            String value = editText.getText().toString();    

            if (value != null && !value.equals("")) //Additional check 
            {    
                if(value.startsWith(".")){
                    editText.setText("0.");
                }
            }
       }

For more info See here how I accomplished this for thousand separator. That may be helpful to you.

Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • @AndrewIndayang Thanks for suggestion but not needed. `afterTextChanged` is called directly when user presses a single character and if that character is DOT then it suddenly sets `0.` if it's not the case of copy and pasting. – Shree Krishna May 17 '16 at 06:55