Skip to content

Instantly share code, notes, and snippets.

@vladislav805
Created December 3, 2016 20:31
Show Gist options
  • Save vladislav805/f11b4e1c9802e856d2e092fdadb4868c to your computer and use it in GitHub Desktop.
Save vladislav805/f11b4e1c9802e856d2e092fdadb4868c to your computer and use it in GitHub Desktop.
Number.prototype.toData = function() {
if (this <= 0) {
return "0 bytes";
};
var n;
for (var i = 5; i >= 0; --i) {
n = Math.round(this / Math.pow(1024, i) * 10) / 10;
if (n >= 1) {
return n + " " + ["b", "KB", "MB", "GB", "TB"][i]);
};
};
return null;
};
// using
var size = 123456;
console.log(size.toData());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment