| 1 |
package net.oni2.aeinstaller.backend; |
| 2 |
|
| 3 |
/** |
| 4 |
* @author Christian Illy |
| 5 |
*/ |
| 6 |
public class SizeFormatter { |
| 7 |
/** |
| 8 |
* @param sizeVal |
| 9 |
* Size in Byte |
| 10 |
* @param digits |
| 11 |
* Number of digits |
| 12 |
* @return Formatted value |
| 13 |
*/ |
| 14 |
public static String format(long sizeVal, int digits) { |
| 15 |
String names[] = { "B", "KiB", "MiB", "GiB", "TiB" }; |
| 16 |
int nameInd = 0; |
| 17 |
float size = sizeVal; |
| 18 |
while ((size > 1024) && (nameInd < (names.length - 1))) { |
| 19 |
nameInd++; |
| 20 |
size /= 1024; |
| 21 |
} |
| 22 |
if (size < 10) |
| 23 |
return String.format( |
| 24 |
"%1." + String.valueOf(Math.max(digits - 1, 0)) + "f %s", |
| 25 |
size, names[nameInd]); |
| 26 |
else if (size < 100) |
| 27 |
return String.format( |
| 28 |
"%1." + String.valueOf(Math.max(digits - 2, 0)) + "f %s", |
| 29 |
size, names[nameInd]); |
| 30 |
else |
| 31 |
return String.format( |
| 32 |
"%1." + String.valueOf(Math.max(digits - 3, 0)) + "f %s", |
| 33 |
size, names[nameInd]); |
| 34 |
} |
| 35 |
} |