Skip to content

Instantly share code, notes, and snippets.

@baughmann
Created December 13, 2020 23:27
Show Gist options
  • Save baughmann/4627c8e04e48a24aa08932e780b3e03b to your computer and use it in GitHub Desktop.
Save baughmann/4627c8e04e48a24aa08932e780b3e03b to your computer and use it in GitHub Desktop.
Typescript: Convert bytes to human friendly string
const bytesToSize = (bytes: number) => {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString());
return Math.round(bytes / Math.pow(1024, i)) + ' ' + sizes[i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment