-3

i can display the size of a app package in MB but i have too much decimal numbers..

 ApplicationInfo appInfo = packageInfo.applicationInfo;

        try{
            File file = new File(appInfo.sourceDir);
            double sizeInBytes = file.length();  // size in Byte
            double sizeInMb = sizeInBytes / (1024 * 1024);
            physicalsize.setText(""+sizeInMb);
        } catch( Exception e ) {
            //e.printStackTrace();
        }

How can i have only 2 decimals like:

4.3 MB instead 4.35652148952

?

David_D
  • 1,404
  • 4
  • 31
  • 65

3 Answers3

0

Wouldn't something like this work?

String.format("%.1f", sizeInMb);
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
0

This should format the double into two decimal places.

DecimalFormat df = new DecimalFormat("#.00"); 
physicalsize.setText(df.format(sizeInMb))

James

James Reeves
  • 97
  • 1
  • 13
0

How about something along the lines of:

double a = 123.4567;
double roundOff = Math.round(a * 10.0) / 10.0;

The resulting output is "123.4"

You could just adapt this to your code.

Aaron C
  • 343
  • 2
  • 9