Skip to content

Instantly share code, notes, and snippets.

@mc-stephen
Forked from zzpmaster/formatBytes.dart
Created November 18, 2022 17:05
Show Gist options
  • Save mc-stephen/e5e88b76137f4158f50ea12047c17c3a to your computer and use it in GitHub Desktop.
Save mc-stephen/e5e88b76137f4158f50ea12047c17c3a 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];
}
@mc-stephen
Copy link
Author

Conver bytes to B,KB,MB,GB.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment