Converting KB to MB, GB, TB dynamically

后端 未结 10 1960
广开言路
广开言路 2020-12-14 18:24
public String size(int size){
    String hrSize = \"\";
    int k = size;
    double m = size/1024;
    double g = size/1048576;
    double t = size/1073741824;

            


        
10条回答
  •  执念已碎
    2020-12-14 19:19

    My basic version (you CAN define some constants instead of computing POW all the time):

    public static String GetFolderSizeHuman(long aBytes)
    {
      if (aBytes < 1024 * 1024) 
        return aBytes + " KB";
      else if (aBytes < Math.pow(2, 20) * 1024)
        return (int) aBytes / Math.pow(2, 20) + " MB";
      else if (aBytes < Math.pow(2, 30) * 1024 )
        return kGbTbFormatter.format(aBytes / Math.pow(2, 30)) + " GB";
      else if (aBytes < Math.pow(2, 40) * 1024)
        return kGbTbFormatter.format(aBytes / Math.pow(2, 40)) + " TB";
    
      else return "N/A (1TB?)";
    }
    

提交回复
热议问题