/**
     *
     * @param bytes 转换得字节
     * @param si 是否需要单位
     * @return
     */
    public static String byteFormat(long bytes, boolean si) {
        String[] units = new String[]{" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"};
        int unit = 1024;
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        double pre = 0;
        if (bytes > 1024) {
            pre = bytes / Math.pow(unit, exp);
        } else {
            pre = (double) bytes / (double) unit;
        }
        if (si) {
            return String.format(Locale.ENGLISH, "%.1f%s", pre, units[(int) exp]);
        }
        return String.format(Locale.ENGLISH, "%.1f", pre);
    }

相关文章:

  • 2022-01-23
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2021-05-22
  • 2021-08-07
  • 2022-03-03
猜你喜欢
  • 2021-06-01
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
  • 2021-12-10
  • 2022-12-23
相关资源
相似解决方案