java文件大小格式化_用Java / JSTL格式化文件大小

小编典典

快速谷歌搜索返回我这个从Appache

Hadoop项目。从那里复制:(Apache许可证,版本2.0):

private static DecimalFormat oneDecimal = new DecimalFormat("0.0");

/**

* Given an integer, return a string that is in an approximate, but human

* readable format.

* It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.

* @param number the number to format

* @return a human readable form of the integer

*/

public static String humanReadableInt(long number) {

long absNumber = Math.abs(number);

double result = number;

String suffix = "";

if (absNumber < 1024) {

// nothing

} else if (absNumber < 1024 * 1024) {

result = number / 1024.0;

suffix = "k";

} else if (absNumber < 1024 * 1024 * 1024) {

result = number / (1024.0 * 1024);

suffix = "m";

} else {

result = number / (1024.0 * 1024 * 1024);

suffix = "g";

}

return oneDecimal.format(result) + suffix;

}

它使用1K = 1024,但是您可以根据需要进行调整。您还需要使用其他DecimalFormat处理<1024情况。

2020-06-08

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值