Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/79c24c1f9248eaf26000 to your computer and use it in GitHub Desktop.
Save ZiTAL/79c24c1f9248eaf26000 to your computer and use it in GitHub Desktop.
bytes converter
var bytes = 1028653056;
//var bytes = 28653056;
//var bytes = 1;
var types = ['Bytes', 'KiloBytes', 'MegaBytes', 'GigaBytes'];
var result;
var index;
for(var i=0;i<types.length;i++)
{
var z = 1;
for(var j=0;j<i;j++)
var z = z * 1024;
var r = (bytes / z).toFixed(2);
if(r>=1)
{
result = r;
index = i;
}
}
console.log(result+" "+types[index]);
/*
Output:
981.00 MegaBytes
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment