Skip to content

Instantly share code, notes, and snippets.

@Oluwasetemi
Forked from lanqy/bytesToSize.js
Created March 12, 2017 09:23
Show Gist options
  • Save Oluwasetemi/96bb29997f4e6f19d0242b5a5325f01e to your computer and use it in GitHub Desktop.
Save Oluwasetemi/96bb29997f4e6f19d0242b5a5325f01e to your computer and use it in GitHub Desktop.
JavaScript To Convert Bytes To MB, KB, Etc
// from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i == 0) return bytes + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment