com.softicar.platform.common.string.formatting.MemoryFormatting Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.string.formatting;
/**
* This class provides static formatting methods for memory.
*
* @author Oliver Richers
*/
public class MemoryFormatting {
private static final double MEMORY_UNIT = 1024;
private static final double MEMORY_UNIT_THRESHOLD = 1 * MEMORY_UNIT;
private static final String[] MEMORY_UNITS = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
public static String formatMemory(long bytes, int precision) {
int unitIndex = 0;
double value = bytes < 0? -bytes : bytes;
while (value >= MEMORY_UNIT_THRESHOLD && unitIndex < MEMORY_UNITS.length - 1) {
value /= MEMORY_UNIT;
++unitIndex;
}
String format = "%." + precision + "f";
return (bytes < 0? "-" : "") + String.format(format, value) + " " + MEMORY_UNITS[unitIndex];
}
}