Skip to content

Instantly share code, notes, and snippets.

@zzpmaster
Last active February 8, 2024 18:51
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zzpmaster/ec51afdbbfa5b2bf6ced13374ff891d9 to your computer and use it in GitHub Desktop.
Save zzpmaster/ec51afdbbfa5b2bf6ced13374ff891d9 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];
}
@spookyuser
Copy link

Very nice! Thank You <3

@4tKnight
Copy link

This works perfectly! Thanks

@vercorro
Copy link

Thanks, very useful for me!!

@cjamcu
Copy link

cjamcu commented Jun 28, 2020

Thanks :)

@giacomomasseron
Copy link

Its not accurate.
With 4351231 bytes it returns 4.15MB instead of 4.35MB

@LostInDarkMath
Copy link

@giacomok The factor is 1024 and not 1000.

@Renkon117
Copy link

Thank you so much!!

@Amanullahgit
Copy link

Cool ❤

@Kamoba
Copy link

Kamoba commented Apr 5, 2021

Yep Nice! Thanks.

@famarrero
Copy link

Perfect

@kirimi
Copy link

kirimi commented Jun 12, 2021

thank you

@erperejildo
Copy link

erperejildo commented Jul 20, 2021

This is not calculated properly as @giacomok mentioned. I'm printing these sizes:

final size = file.lengthSync() / 1000000;
print(size);
print(formatBytes(file.lengthSync()));

and getting this:

I/flutter ( 4457): 2.370935
I/flutter ( 4457): 2.26 MB

The image is 2.37, not 2.26:

Screenshot 2021-07-20 at 12 08 00

@saidani300
Copy link

thank you

@masteradit
Copy link

masteradit commented Aug 17, 2021

Thank you! This was really helpful.

I was also facing the same issue as @erperejildo mentioned. So I used 1000 instead of 1024.

Edit: just realised @LostInDarkMath mentioned it in his comment. Thanks!

@bllaw
Copy link

bllaw commented Sep 5, 2021

Nice!

@imaNNeo
Copy link

imaNNeo commented Apr 22, 2022

Thanks :)

@agavrel
Copy link

agavrel commented Nov 17, 2022

Thanks, very handy!

@mc-stephen
Copy link

Thanks alot for this bud, you really did us well

@Coimbra1984
Copy link

See https://en.wikipedia.org/wiki/Binary_prefix, to be fully compliant, factor 1024 requires abbreviations ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"], also note that in offical SI-Notation kB (kilo byte) has a lower case k, while KiB has not.

String formatBytes(int bytes, int decimals, bool binaryPrefixes) {
if (bytes <= 0) return "0 B";
int fac = 1000;
List suffixes = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if (binaryPrefixes) {
fac = 1024;
suffixes = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
}

var i = (log(bytes) / log(fac)).floor();
i = i >= (suffixes.length - 1) ? suffixes.length - 1 : i;
return '${(bytes / pow(fac, i)).toStringAsFixed(decimals)} ${suffixes[i]}';
}

@RoyalCoder88
Copy link

thanks a lot

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