Skip to content

Instantly share code, notes, and snippets.

@trevershick
Created February 23, 2015 03:56
Show Gist options
  • Save trevershick/737205b0ba79d8877a43 to your computer and use it in GitHub Desktop.
Save trevershick/737205b0ba79d8877a43 to your computer and use it in GitHub Desktop.
// thanks https://gist.github.com/thomseddon/3511330
function formatBytes(bytes, precision) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
if (typeof precision === 'undefined') precision = 1;
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
@JediMadeThis
Copy link

great :D

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