Skip to content

Commit

Permalink
Improved formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Mar 31, 2020
1 parent 333a3cc commit 50db508
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/main/java/mpo/dayon/common/utils/UnitUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ public static String toBitSize(double bits) {
}

public enum ByteUnit {
K("K", "kilo", Math.pow(2, 10), 0),
M("M", "mega", Math.pow(2, 20),0),
G("G", "giga", Math.pow(2, 30),1),
T("T", "tera", Math.pow(2, 40),1),
P("P", "peta", Math.pow(2, 50),2),
E("E", "exa", Math.pow(2, 60),2),
Z("Z", "zetta", Math.pow(2, 70), 3),
Y("Y", "yotta", Math.pow(2, 80), 3);
K("K", "kilo", Math.pow(2, 10), "%.0f K"),
M("M", "mega", Math.pow(2, 20),"%.0f M"),
G("G", "giga", Math.pow(2, 30),"%.1f G"),
T("T", "tera", Math.pow(2, 40),"%.1f T"),
P("P", "peta", Math.pow(2, 50),"%.2f P"),
E("E", "exa", Math.pow(2, 60),"%.2f E"),
Z("Z", "zetta", Math.pow(2, 70), "%.3f Z"),
Y("Y", "yotta", Math.pow(2, 80), "%.3f Y");

private final String symbol;
private final String name;
private final double value;
private final int decimals;
private final String formatter;

ByteUnit(String symbol, String name, double value, int decimals) {
ByteUnit(String symbol, String name, double value, String formatter) {
this.symbol = symbol;
this.name = name;
this.value = value;
this.decimals = decimals;
this.formatter = formatter;
}
}

Expand All @@ -74,7 +74,7 @@ static String toByteSize(double bytes, boolean withDecimal) {
if (withDecimal) {
return String.format(DEC_UNIT, bytes / unit.value, unit.symbol);
}
return String.format("%." + unit.decimals + "f %s", bytes / unit.value, unit.symbol);
return String.format(unit.formatter, bytes / unit.value);
}
}

Expand Down

0 comments on commit 50db508

Please sign in to comment.