动态地将KB转换为MB,GB,TB

public String size(int size){ String hrSize = ""; int k = size; double m = size/1024; double g = size/1048576; double t = size/1073741824; DecimalFormat dec = new DecimalFormat("0.00"); if (k>0) { hrSize = dec.format(k).concat("KB"); } if (m>0) { hrSize = dec.format(m).concat("MB"); } if (g>0) { hrSize = dec.format(g).concat("GB"); } if (t>0) { hrSize = dec.format(t).concat("TB"); } return hrSize; } 

这是一种应返回GB,MB,KB或TB大小的方法。 输入值以KB为单位。 例如1245的结果应该像1.21MB,但我得到的是1.00MB。

您正在执行integer division 。 所以除法的结果也是integer 。 小数部分被截断。

 so, 1245 / 1024 = 1 

将您的部门更改为floating point division : –

 double m = size/1024.0; double g = size/1048576.0; double t = size/1073741824.0; 

此外,您的比较是错误的。 你应该用1进行比较。

 if (m > 1), if (t > 1), if (g > 1) 

理想情况下,我会将您的比较改为: –

  if (t > 1) { hrSize = dec.format(t).concat("TB"); } else if (g > 1) { hrSize = dec.format(g).concat("GB"); } else if (m > 1) { hrSize = dec.format(m).concat("MB"); } else { hrSize = dec.format(size).concat("KB"); } 

您需要先与较高的单位进行比较,然后再移到较低的单位。

修改后的版本。 只调用一次格式。 包括“字节”。

 public static String formatFileSize(long size) { String hrSize = null; double b = size; double k = size/1024.0; double m = ((size/1024.0)/1024.0); double g = (((size/1024.0)/1024.0)/1024.0); double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0); DecimalFormat dec = new DecimalFormat("0.00"); if ( t>1 ) { hrSize = dec.format(t).concat(" TB"); } else if ( g>1 ) { hrSize = dec.format(g).concat(" GB"); } else if ( m>1 ) { hrSize = dec.format(m).concat(" MB"); } else if ( k>1 ) { hrSize = dec.format(k).concat(" KB"); } else { hrSize = dec.format(b).concat(" Bytes"); } return hrSize; } 

我喜欢这个:

 public static String getDynamicSpace(long diskSpaceUsed) { if (diskSpaceUsed <= 0) { return "0"; } final String[] units = new String[] { "B", "KiB", "MiB", "GiB", "TiB" }; int digitGroups = (int) (Math.log10(diskSpaceUsed) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(diskSpaceUsed / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; } 

问题是你正在使用整数除法。 将您的代码更改为:

 double m = size/1024.0; double g = size/1048576.0; double t = size/1073741824.0; 

在原始代码中, double m = size/1024将整数size除以1024 ,将结果截断为整数,然后才将其转换为double 。 这就是分数部分迷失的原因。

你正在执行整数除法

即,31/15将导致2,而不是2.whatever

只需用Dd附加数字,表示它是一个双倍,你会没事的

 double m = size/1024D; double g = size/1048576D; double t = size/1073741824D; 

要做到这一点并不容易。 Rohit Jain提到了整数运算。 舍入也可能是一个问题,因为总是向下舍入可能是不可取的。 我建议去寻找像triava库一样的可用解决方案。

它可以在3种不同的系统(SI,IEC,JEDEC)和各种输出选项中以任意精度格式化数字。 以下是triavaunit testing的一些代码示例:

 UnitFormatter.formatAsUnit(1126, UnitSystem.SI, "B"); // = "1.13kB" UnitFormatter.formatAsUnit(2094, UnitSystem.IEC, "B"); // = "2.04KiB" 

打印精确千克,超值(此处W =瓦特):

 UnitFormatter.formatAsUnits(12_000_678, UnitSystem.SI, "W", ", "); // = "12MW, 678W" 

您可以传递DecimalFormat来自定义输出:

 UnitFormatter.formatAsUnit(2085, UnitSystem.IEC, "B", new DecimalFormat("0.0000")); // = "2.0361KiB" 

对于kilo或mega值的任意操作,您可以将它们拆分为组件:

 UnitComponent uc = new UnitComponent(123_345_567_789L, UnitSystem.SI); int kilos = uc.kilo(); // 567 int gigas = uc.giga(); // 123 
 String[] fileSizeUnits = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; public String calculateProperFileSize(double bytes){ String sizeToReturn = "";// = FileUtils.byteCountToDisplaySize(bytes), unit = ""; int index = 0; for(index = 0; index < fileSizeUnits.length; index++){ if(bytes < 1024){ break; } bytes = bytes / 1024; } System.out.println("Systematic file size: " + bytes + " " + fileSizeUnits[index]); sizeToReturn = String.valueOf(bytes) + " " + fileSizeUnits[index]; return sizeToReturn; } 

只需添加更多文件单元(如果有任何缺失),您将看到单位大小达到该单位(如果您的文件有这么长的长度)

我的基本版本(你可以定义一些常量而不是一直计算POW):

 public static String GetFolderSizeHuman(long aBytes) { if (aBytes < 1024 * 1024) return aBytes + " KB"; else if (aBytes < Math.pow(2, 20) * 1024) return (int) aBytes / Math.pow(2, 20) + " MB"; else if (aBytes < Math.pow(2, 30) * 1024 ) return kGbTbFormatter.format(aBytes / Math.pow(2, 30)) + " GB"; else if (aBytes < Math.pow(2, 40) * 1024) return kGbTbFormatter.format(aBytes / Math.pow(2, 40)) + " TB"; else return "N/A (1TB?)"; } 

bickster的答案工作得很好,但问题是它返回45.00 Bytes12.00 KB 。 在我看来,如果它们是零,则应删除最后的十进制数字。 因此,不是45.00 Bytes12.00 KB ,而是45 B12 KB (注意Bytes已经改为B这只是为了统一,因为我们有KB,MB等而不是Kilobytes,兆字节等)。

 private boolean isDouble(double value) { if (value % 1 == 0) { Log.d(TAG, "value is " + value + " and is not double"); return false; } else { Log.d(TAG, "value is " + value + " and is double"); return true; } } 

上述方法只是检查该值是否为十进制数字。

 private String formatFileSize(long size) { String hrSize = null; double b = size; double k = size/1024.0; double m = ((size/1024.0)/1024.0); double g = (((size/1024.0)/1024.0)/1024.0); double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0); DecimalFormat dec1 = new DecimalFormat("0.00"); DecimalFormat dec2 = new DecimalFormat("0"); if (t>1) { hrSize = isDouble(t) ? dec1.format(t).concat(" TB") : dec2.format(t).concat(" TB"); } else if (g>1) { hrSize = isDouble(g) ? dec1.format(g).concat(" GB") : dec2.format(g).concat(" GB"); } else if (m>1) { hrSize = isDouble(m) ? dec1.format(m).concat(" MB") : dec2.format(m).concat(" MB"); } else if (k>1) { hrSize = isDouble(k) ? dec1.format(k).concat(" KB") : dec2.format(k).concat(" KB"); } else { hrSize = isDouble(b) ? dec1.format(b).concat(" B") : dec2.format(b).concat(" B"); } return hrSize; }