Do not display fractional bytes

Also do not allocate String array on every invocation.
This commit is contained in:
Andrew Gaul 2013-04-11 19:26:03 -07:00
parent d2c8f0de68
commit 152d0963db

View File

@ -153,15 +153,17 @@ public class AppDetails extends ListActivity {
} }
} }
private static final String[] FRIENDLY_SIZE_FORMAT = {
"%.0f B", "%.0f KiB", "%.1f MiB", "%.2f GiB" };
private static String getFriendlySize(int size) { private static String getFriendlySize(int size) {
double s = size; double s = size;
String[] format = { "%f B", "%.0f KiB", "%.1f MiB", "%.2f GiB" };
int i = 0; int i = 0;
while (i < format.length - 1 && s >= 1024) { while (i < FRIENDLY_SIZE_FORMAT.length - 1 && s >= 1024) {
s = (100 * s / 1024) / 100.0; s = (100 * s / 1024) / 100.0;
i++; i++;
} }
return String.format(format[i], s); return String.format(FRIENDLY_SIZE_FORMAT[i], s);
} }
private static final int INSTALL = Menu.FIRST; private static final int INSTALL = Menu.FIRST;