格式文件大小为MB,GB等 [英] Format file size as MB, GB etc
问题描述
eg
1L ==> 1B;
1024L ==> 1 KB;
2537253L ==> 2.3 MB
等。
我找到了这个以前的答案,我没有找到满意的
我已经想出了自己的解决方案,它有类似的缺点:$ b
$ b pre code private静态最终长K = 1024;
private static final long M = K * K;
private static final G = M * K;
private static final long T = G * K;
public static String convertToStringRepresentation(final long value){
final long [] dividers = new long [] {T,G,M,K,1};
final String [] units = new String [] {TB,GB,MB,KB,B};
if(value <1)
throw new IllegalArgumentException(Invalid file size:+ value);
String result = null;
for(int i = 0; i< divrs.length; i ++){
final long divider = divider [i];
if(value> = divider){
result = format(value,divider,units [i]);
break;
}
}
返回结果;
private static String格式(最终长整型值,
最终长整型数,
最终整型单位){
最终双重结果=
divider> 1? (双)值/(双)分频器:(双)值;
return String.format(%。1f%s,Double.valueOf(result),unit);
}
主要问题是我对Decimalformat和/或String.format知之甚少。
我想1024L,1025L等映射到 1 KB
而不是 1.0 KB
。
所以,有两种可能:
- 我宁愿选择一个好的out-of-如apache commons或google guava。
- 如果没有,有人可以告诉我如何摆脱.0部分(不采用字符串替换和正则表达式,我可以自己做)
public static String readableFileSize(long size){
if(size< = 0)返回0;
final String [] units = new String [] {B,kB,MB,GB,TB};
int digitGroups =(int)(Math.log10(size)/Math.log10(1024));
返回新的DecimalFormat(#,## 0。#)。format(size / Math.pow(1024,digitGroups))++ units [digitGroups];
$ b $ p $这样可以达到1000 TB ....程序很短!
I need to display a file size as String using sensible units.
e.g.
1L ==> "1 B";
1024L ==> "1 KB";
2537253L ==> "2.3 MB"
etc.
I found this previous answer, which I didn't find satisfactory
I have come up with my own solution which has similar shortcomings:
private static final long K = 1024;
private static final long M = K * K;
private static final long G = M * K;
private static final long T = G * K;
public static String convertToStringRepresentation(final long value){
final long[] dividers = new long[] { T, G, M, K, 1 };
final String[] units = new String[] { "TB", "GB", "MB", "KB", "B" };
if(value < 1)
throw new IllegalArgumentException("Invalid file size: " + value);
String result = null;
for(int i = 0; i < dividers.length; i++){
final long divider = dividers[i];
if(value >= divider){
result = format(value, divider, units[i]);
break;
}
}
return result;
}
private static String format(final long value,
final long divider,
final String unit){
final double result =
divider > 1 ? (double) value / (double) divider : (double) value;
return String.format("%.1f %s", Double.valueOf(result), unit);
}
The main problem is my limited knowledge of Decimalformat and / or String.format.
I would like 1024L, 1025L etc to map to 1 KB
rather than 1.0 KB
.
So, two possibilities:
- I would prefer a good out-of-the-box solution in a public library like apache commons or google guava.
- If there isn't, can someone show me how to get rid of the '.0' part (without resorting to string replacement and regex, I can do that myself)
解决方案 public static String readableFileSize(long size) {
if(size <= 0) return "0";
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
This will work up to 1000 TB.... and the program is short!
这篇关于格式文件大小为MB,GB等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!