0

I have a method to get any app's size. But the problem is I always get .00 value after the decimal.

Suppose, an app's size is 3.44

So, if I call my method then it will return a value of 3.00 And, it is same for every application. If any app's size is less than 1 mb, then I get 0.00. Please help me. Here is my method

    private String getAppsSize() {
        File cApp;
        DecimalFormat cDFormat = new DecimalFormat("#.##");
        double appSizeInMegaBytes;
        String formattedText = null;
        try {
            cAInfo = cPManager.getApplicationInfo(getCurrentAppPackageName(), 0);
            cApp = new File(cAInfo.publicSourceDir);
            appSizeInMegaBytes = cApp.length() / 1048576;
            cDFormat.format(appSizeInMegaBytes);
            formattedText = Double.toString(appSizeInMegaBytes);
        }
 catch (PackageManager.NameNotFoundException e) {
            showToast("Failed to count app size!");
            e.printStackTrace();
        }
        return formattedText;
    }

Here, I have a method getCurrentPackageName(). It causes no error. Because I am using it somewhere else. It works fine. So, now I don't know where my problem occurs...

Vucko
  • 7,371
  • 2
  • 27
  • 45
Muhammad Jobayer
  • 157
  • 1
  • 10

3 Answers3

0

Your issue is that cApp.length() is returning a long, and then you are dividing that by another whole number, so you aren't going to get any decimal places that way. Try dividing by another double instead. So something like:

double megaByte = 1048576;
appSizeInMegaBytes = cApp.length() / megaByte;

Just adding a decimal to your divisor should also work: appSizeInMegaBytes = cApp.length() / 1048576.0;

NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • Ok, it's working. But, I am getting a lot of value after decimal. Now, if I call the method, so I am getting 3.0256983 (example). But, I want last two digits only after decimal – Muhammad Jobayer Mar 28 '16 at 15:33
0

I always rely on the good old implicit conversion of strings:

            formattedText = ""+appSizeInMegaBytes;

That's how you'll see if the value is good.

Vucko
  • 7,371
  • 2
  • 27
  • 45
0

You're throwing out your formatted text in these lines:

cDFormat.format(appSizeInMegaBytes);
formattedText = Double.toString(appSizeInMegaBytes);

The Format.format() call will return a string, so don't toss it out. Try this instead:

 formattedText = cDFormat.format(appSizeInMegaBytes);
Michael Peacock
  • 2,011
  • 1
  • 11
  • 14