0

I have a decimal number in EditText and I'm trying to change it to always show a decimal part but the user doesn't be able to change the decimal part, only the integer part has to be editable. The decimal part is always a default value.

Example: I have the number 2.025,50 at EditText, if I delete all the digits Ill have 0,50. If I write 10 , Ill have 10,50.

Can anyone help me out ??

Ênio Abrantes
  • 302
  • 2
  • 11

1 Answers1

0

I created a function you can use for this, so you just input your number with a decimal and it will give you the decimal part of the number. Use editText changed listener. So when u pass the value being typed by a user call this function and pass the numberWithTheFraction to the function getFractionalPart and add the userinput as shown in the code bellow.

   private static double getFractionalPart(double num) {     
       if (num > 0) {
          return num - Math.floor(num);
       } else {
          return ((num - Math.ceil(num)) * -1);
}
}

you can have a look at this example for an example of editText change listener.

So in the above example when you say textView.setText(getFractionalPart(numberWithTheFraction)+userInput)

DvixExtract
  • 1,275
  • 15
  • 25