Skip to content

Instantly share code, notes, and snippets.

@Cranked
Created June 26, 2022 10:24
Show Gist options
  • Save Cranked/f01dcf488a60d897e08359754311fb84 to your computer and use it in GitHub Desktop.
Save Cranked/f01dcf488a60d897e08359754311fb84 to your computer and use it in GitHub Desktop.
Convert bytes to KB,MB,GB,TB,PB,EB,ZB,YB
public String getFileSize(long size, int round) {
if (size <= 0)
return "0";
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return String.format("%." + round + "f", size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment