java B转换KB MB GB TB PB EB ZB

  1. public static String readableFileSize(long size) {  
  2.         if (size <= 0) {  
  3.             return "0";  
  4.         }  
  5.         final String[] units = new String[]{"B", "kB", "MB", "GB", "TB", "PB","EB","ZB"};  
  6.         int digitGroups = (int) (Math.log10(size) / Math.log10(1024));  
  7.         return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];  
  8.     }  

     

  1. public static void main(String[] args) {  
  2.         String str= "212132342412";
  3.         String str=MyCommonTool.readableFileSize(Long.parseLong(str)+'l');  
  4.         System.out.println("str="+str);                      
  5.           
  6.     }  
原文地址:https://www.cnblogs.com/yaomajor/p/6085671.html