Skip to content

Instantly share code, notes, and snippets.

@jkfrancis06
Forked from zzpmaster/formatBytes.dart
Created January 4, 2022 11:30
Show Gist options
  • Select an option

  • Save jkfrancis06/a1f521b65c49868ed6726340fc1313f1 to your computer and use it in GitHub Desktop.

Select an option

Save jkfrancis06/a1f521b65c49868ed6726340fc1313f1 to your computer and use it in GitHub Desktop.
convert bytes to kb mb in dart
static String formatBytes(int bytes, int decimals) {
if (bytes <= 0) return "0 B";
const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
var i = (log(bytes) / log(1024)).floor();
return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) +
' ' +
suffixes[i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment