2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Convert byte to required MB, GB or TB etc using Java

Convert byte to required MB, GB or TB etc using Java. How to do?

userimage

Try below code to get the result

public static String convertSize(long size) {
   if (size <= 0) {
      return "0";
   }
   final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
   int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
   return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
Answer is