字节转 B KB MB GB TB

2022-08-05  本文已影响0人  指下光年
public static String readFileSize(int size){
      if(size<=0){
         return "0";
      }
      final String[] units = new String[]{"B","KB","MB","GB","TB"};
      int digitGroup = (int)(Math.log10(size)/Math.log10(1024));
      double s = size/Math.pow(1024,digitGroup);
      return new DecimalFormat("#,##0.#").format(s)+units[digitGroup];
}

public static void main(String[] args) {
     String s = readFileSize(52428800);
     System.out.println(s);
}
上一篇下一篇

猜你喜欢

热点阅读