【问题标题】:How to fix RAM format size?如何修复 RAM 格式大小?
【发布时间】:2018-05-31 11:58:08
【问题描述】:

如果 RAM 超过 1 GB,则它不会显示确切的 RAM。

如何重新格式化?

 public static String formatSize(long size) {
    String suffix = null;

    if (size >= 1024) {
        suffix = " KB ";
        size /= 1024;
        if (size >= 1024) {
            suffix = " MB ";
            size /= 1024;
        }
    }

【问题讨论】:

  • 不清楚它显示什么以及您希望它显示什么。举个例子
  • 什么是 KB?开尔文字节?看这里en.wikipedia.org/wiki/Binary_prefix,应该是kB或者KiB。
  • 请发布方法的完整代码以及如何调用它。当前代码不返回任何内容。
  • 看起来您正在为每个 if 块内的结果添加后缀

标签: java android ram


【解决方案1】:

试试这个

private static String formatSize(long size) {
        long Kb = 1024;
        long Mb = Kb * 1024;
        long Gb = Mb * 1024;
        long Tb = Gb * 1024;
        long Pb = Tb * 1024;
        long Eb = Pb * 1024;

        if (size < Kb) return floatForm(size) + " byte";
        if (size >= Kb && size < Mb) return floatForm((double) size / Kb) + " Kb";
        if (size >= Mb && size < Gb) return floatForm((double) size / Mb) + " Mb";
        if (size >= Gb && size < Tb) return floatForm((double) size / Gb) + " Gb";
        if (size >= Tb && size < Pb) return floatForm((double) size / Tb) + " Tb";
        if (size >= Pb && size < Eb) return floatForm((double) size / Pb) + " Pb";
        if (size >= Eb) return floatForm((double) size / Eb) + " Eb";

        return "???";
    }

 private static String floatForm(double d) {
        return new DecimalFormat("#.##").format(d);
    }

【讨论】:

  • 供您参考,MbMegabitMBMegaByte
猜你喜欢
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
相关资源
最近更新 更多