mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
DataSizeUtil 新增format方法(issue#IB6UUX@Gitee)
This commit is contained in:
parent
d63410e432
commit
ad9c361fc4
@ -144,7 +144,7 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
if(null == unit){
|
if(null == unit){
|
||||||
unit = DataUnit.BYTES;
|
unit = DataUnit.BYTES;
|
||||||
}
|
}
|
||||||
return new DataSize(Math.multiplyExact(amount, unit.size().toBytes()));
|
return new DataSize(Math.multiplyExact(amount, unit.getSize().toBytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -159,7 +159,7 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
if(null == unit){
|
if(null == unit){
|
||||||
unit = DataUnit.BYTES;
|
unit = DataUnit.BYTES;
|
||||||
}
|
}
|
||||||
return new DataSize(amount.multiply(new BigDecimal(unit.size().toBytes())).longValue());
|
return new DataSize(amount.multiply(new BigDecimal(unit.getSize().toBytes())).longValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.io.unit;
|
package org.dromara.hutool.core.io.unit;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
@ -79,4 +80,20 @@ public class DataSizeUtil {
|
|||||||
return new DecimalFormat("#,##0." + StrUtil.repeat('#', scale))
|
return new DecimalFormat("#,##0." + StrUtil.repeat('#', scale))
|
||||||
.format(size / Math.pow(1024, digitGroups)) + delimiter + unitNames[digitGroups];
|
.format(size / Math.pow(1024, digitGroups)) + delimiter + unitNames[digitGroups];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据单位,将文件大小转换为对应单位的大小
|
||||||
|
*
|
||||||
|
* @param size 文件大小
|
||||||
|
* @param fileDataUnit 单位
|
||||||
|
* @return 大小
|
||||||
|
* @since 5.8.34
|
||||||
|
*/
|
||||||
|
public static String format(final Long size, final DataUnit fileDataUnit){
|
||||||
|
if (size <= 0) {
|
||||||
|
return "0";
|
||||||
|
}
|
||||||
|
final int digitGroups = ArrayUtil.indexOf(DataUnit.UNIT_NAMES,fileDataUnit.getSuffix());
|
||||||
|
return new DecimalFormat("##0.##").format(size / Math.pow(1024, digitGroups)) + " " + DataUnit.UNIT_NAMES[digitGroups];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,16 +70,35 @@ public enum DataUnit {
|
|||||||
public static final String[] UNIT_NAMES_SIMPLE = new String[]{"B", "K", "M", "G", "T", "P", "E"};
|
public static final String[] UNIT_NAMES_SIMPLE = new String[]{"B", "K", "M", "G", "T", "P", "E"};
|
||||||
|
|
||||||
private final String suffix;
|
private final String suffix;
|
||||||
|
|
||||||
private final DataSize size;
|
private final DataSize size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param suffix 单位后缀
|
||||||
|
* @param size 单位大小
|
||||||
|
*/
|
||||||
DataUnit(final String suffix, final DataSize size) {
|
DataUnit(final String suffix, final DataSize size) {
|
||||||
this.suffix = suffix;
|
this.suffix = suffix;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
DataSize size() {
|
/**
|
||||||
|
* 单位后缀
|
||||||
|
*
|
||||||
|
* @return 单位后缀
|
||||||
|
* @since 5.8.34
|
||||||
|
*/
|
||||||
|
public String getSuffix() {
|
||||||
|
return this.suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单位大小
|
||||||
|
*
|
||||||
|
* @return 单位大小
|
||||||
|
*/
|
||||||
|
DataSize getSize() {
|
||||||
return this.size;
|
return this.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user