Converting KB to MB, GB, TB dynamically
You are performing integer division
. So the result of division is also integer
. And fractional part is truncated.
so, 1245 / 1024 = 1
Change your division to floating point division
: -
double m = size/1024.0;
double g = size/1048576.0;
double t = size/1073741824.0;
Also, your comparison is faulty. You should do the comparison with 1
.
if (m > 1), if (t > 1), if (g > 1)
Ideally I would change your comparison to: -
if (t > 1) {
hrSize = dec.format(t).concat("TB");
} else if (g > 1) {
hrSize = dec.format(g).concat("GB");
} else if (m > 1) {
hrSize = dec.format(m).concat("MB");
} else {
hrSize = dec.format(size).concat("KB");
}
You need to compare with the higher unit first, and then move to the lower one.
The problem is that you're using integer division. Change your code to:
double m = size/1024.0;
double g = size/1048576.0;
double t = size/1073741824.0;
In your original code, double m = size/1024
would divide the integer size
by 1024
, truncate the result to an integer, and only then convert it to double
. This is why the fractional part was getting lost.
A modified version. Only calls format once. Includes "Bytes".
public static String formatFileSize(long size) {
String hrSize = null;
double b = size;
double k = size/1024.0;
double m = ((size/1024.0)/1024.0);
double g = (((size/1024.0)/1024.0)/1024.0);
double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0);
DecimalFormat dec = new DecimalFormat("0.00");
if ( t>1 ) {
hrSize = dec.format(t).concat(" TB");
} else if ( g>1 ) {
hrSize = dec.format(g).concat(" GB");
} else if ( m>1 ) {
hrSize = dec.format(m).concat(" MB");
} else if ( k>1 ) {
hrSize = dec.format(k).concat(" KB");
} else {
hrSize = dec.format(b).concat(" Bytes");
}
return hrSize;
}
I love this:
public static String getDynamicSpace(long diskSpaceUsed)
{
if (diskSpaceUsed <= 0) {
return "0";
}
final String[] units = new String[] { "B", "KiB", "MiB", "GiB", "TiB" };
int digitGroups = (int) (Math.log10(diskSpaceUsed) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(diskSpaceUsed / Math.pow(1024, digitGroups))
+ " " + units[digitGroups];
}