Last active
June 16, 2017 08:00
-
-
Save culmat/9a43bc1de2aee31b9a80 to your computer and use it in GitHub Desktop.
Format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package common; | |
public class Format { | |
private static final String[] BYTE_UNITS = new String[] { "b", "Kb", "Mb", "Gb" }; | |
private static final int[] BYTE_STEPS = new int[] { 1024, 1024, 1024, 1024 }; | |
private static final String[] TIME_UNITS = new String[] { "msec", "sec", "min", "h", "day", "week", "year" }; | |
private static final int[] TIME_STEPS = new int[] { 1000, 60, 60, 24, 7, 52, 100 }; | |
public static String format(String[] unit, int[] step, double value) { | |
int i = 0; | |
while (i < unit.length-1 && value >= step[i]) { | |
value = value / step[i++]; | |
} | |
return String.format("%2.2f", value) + " " + unit[i]; | |
} | |
public static String bytes(double value) { | |
return format(BYTE_UNITS, BYTE_STEPS, value); | |
} | |
public static String time(double value) { | |
return format(TIME_UNITS, TIME_STEPS, value); | |
} | |
public static String nanotime(double value) { | |
return time(value / 1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment