1

I already searched online and all that I found is using DecimalFormat and I tried it, but when I code on Android Studio, appears a message saying that is necessary to use the API 24 to this kind of command. The API 24 has erros and all the sites I looked, advised to use the API 23.

So, I need a way to show double numbers only with two decimal digits on my AlertDialog.

shilovk
  • 11,718
  • 17
  • 75
  • 74
Mekillerow
  • 65
  • 3
  • 9

2 Answers2

4

referring to this SO post, you can use this code:

String formattedValue = String.format("%.2f", myDouble);

Let me know if it is what you were looking for

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
0
 /** Round on two digits after "." */
    public static float round(float f){
        return Math.round(f * 100) / 100.0F;
    }

P.S. Use float for short float numbers. Thats what its made for.

Edit: "Ok, with 3 decimal digits its the better too,right? How can I limit those digits?"

Take a quick look how it works for two decimal places. Then its really simple to expand for three.

XxGoliathusxX
  • 922
  • 13
  • 34