zoukankan      html  css  js  c++  java
  • 文件大小转换带上单位工具类(文件byte自动转KBMBGB)

       /**
         *
         * @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);
        }
    
  • 相关阅读:
    【设计模式
    【JavaEE】之SSM入门项目的搭建
    【Android
    【Android
    【Android
    【Android
    【Android
    【Android
    随风 随意
    优秀代码所具备的品质
  • 原文地址:https://www.cnblogs.com/proper128/p/13100552.html
Copyright © 2011-2022 走看看