mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:20:07 +08:00
DataSizeUtil 新增format方法(issue#IB6UUX@Gitee)
This commit is contained in:
parent
50807c84d6
commit
e6c6baabd2
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.34(2024-11-24)
|
||||
# 5.8.34(2024-11-25)
|
||||
|
||||
### 🐣新特性
|
||||
* 【http 】 增加Windows微信浏览器识别(issue#IB3SJF@Gitee)
|
||||
@ -10,6 +10,7 @@
|
||||
* 【core 】 Calculator兼容`x`字符作为乘号(issue#3787@Github)
|
||||
* 【poi 】 Excel07SaxReader中,对于小数类型,增加精度判断(issue#IB0EJ9@Gitee)
|
||||
* 【extra 】 SpringUtil增加getBean重载(issue#3779@Github)
|
||||
* 【core 】 DataSizeUtil 新增format方法(issue#IB6UUX@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复DateUtil.rangeToList中step小于等于0时无限循环问题(issue#3783@Github)
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.io.unit;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
@ -35,4 +37,20 @@ public class DataSizeUtil {
|
||||
return new DecimalFormat("#,##0.##")
|
||||
.format(size / Math.pow(1024, digitGroups)) + " " + DataUnit.UNIT_NAMES[digitGroups];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据单位,将文件大小转换为对应单位的大小
|
||||
*
|
||||
* @param size 文件大小
|
||||
* @param fileDataUnit 单位
|
||||
* @return 大小
|
||||
* @since 5.8.34
|
||||
*/
|
||||
public static String format(Long size, DataUnit fileDataUnit){
|
||||
if (size <= 0) {
|
||||
return "0";
|
||||
}
|
||||
int digitGroups = ArrayUtil.indexOf(DataUnit.UNIT_NAMES,fileDataUnit.getSuffix());
|
||||
return new DecimalFormat("##0.##").format(size / Math.pow(1024, digitGroups)) + " " + DataUnit.UNIT_NAMES[digitGroups];
|
||||
}
|
||||
}
|
||||
|
@ -44,10 +44,12 @@ public enum DataUnit {
|
||||
*/
|
||||
TERABYTES("TB", DataSize.ofTerabytes(1));
|
||||
|
||||
/**
|
||||
* 单位后缀
|
||||
*/
|
||||
public static final String[] UNIT_NAMES = new String[]{"B", "KB", "MB", "GB", "TB", "PB", "EB"};
|
||||
|
||||
private final String suffix;
|
||||
|
||||
private final DataSize size;
|
||||
|
||||
|
||||
@ -56,6 +58,16 @@ public enum DataUnit {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单位后缀
|
||||
*
|
||||
* @return 单位后缀
|
||||
* @since 5.8.34
|
||||
*/
|
||||
public String getSuffix() {
|
||||
return this.suffix;
|
||||
}
|
||||
|
||||
DataSize size() {
|
||||
return this.size;
|
||||
}
|
||||
@ -76,5 +88,4 @@ public enum DataUnit {
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown data unit suffix '" + suffix + "'");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package cn.hutool.core.io.unit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DataSizeUtilTest {
|
||||
|
||||
@Test
|
||||
@ -61,6 +62,18 @@ public class DataSizeUtilTest {
|
||||
assertEquals("1 TB", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatWithUnitTest(){
|
||||
String format = DataSizeUtil.format(Long.MAX_VALUE, DataUnit.TERABYTES);
|
||||
assertEquals("8388608 TB", format);
|
||||
|
||||
format = DataSizeUtil.format(1024L * 1024 * 1024 * 1024 * 1024, DataUnit.GIGABYTES);
|
||||
assertEquals("1048576 GB", format);
|
||||
|
||||
format = DataSizeUtil.format(1024L * 1024 * 1024 * 1024, DataUnit.GIGABYTES);
|
||||
assertEquals("1024 GB", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void issueI88Z4ZTest() {
|
||||
final String size = DataSizeUtil.format(10240000);
|
||||
|
Loading…
Reference in New Issue
Block a user