fix date size bug

This commit is contained in:
Looly
2020-09-08 15:42:29 +08:00
parent 4d7b795505
commit fd51eb08b4
3 changed files with 9 additions and 2 deletions

View File

@@ -31,7 +31,7 @@ public class DataSizeUtil {
if (size <= 0) {
return "0";
}
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
int digitGroups = Math.min(DataUnit.UNIT_NAMES.length-1, (int) (Math.log10(size) / Math.log10(1024)));
return new DecimalFormat("#,##0.##")
.format(size / Math.pow(1024, digitGroups)) + " " + DataUnit.UNIT_NAMES[digitGroups];
}

View File

@@ -19,4 +19,10 @@ public class DataSizeUtilTest {
parse = DataSizeUtil.parse("3mb");
Assert.assertEquals(3145728, parse);
}
@Test
public void formatTest(){
final String format = DataSizeUtil.format(Long.MAX_VALUE);
Assert.assertEquals("8,192 EB", format);
}
}