0

I have a custom keyboard and I try to convert input value like a double. For example, input 1 is 0.01. My main goal is to show only two elements after a dot, always, but if the input is 80 then result is 0.8 ,but I want it to be 0.80 Here is my source

 userEntered = userEntered + pressedString;
 Double dd = Double.parseDouble(userEntered);
 dd = dd / 100;
 amountText.setText(dd.toString());

I clearing input value like this in a button click

if (userEntered.length() > 0) {
    userEntered = userEntered.substring(0, userEntered.length() - 1);
    Double dd = Double.parseDouble(userEntered);
    dd = dd / 100;
    amountText.setText(dd.toString());
}

How I can solve a problem? Thanks everyone

Onur A.
  • 3,007
  • 3
  • 22
  • 37
BekaKK
  • 2,173
  • 6
  • 42
  • 80

1 Answers1

1

You can use DecimalFormat class in Java.

Please have look at https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

Example :

DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(yourDouble));
Mujahid Masood
  • 111
  • 2
  • 8