Last active
August 29, 2015 14:16
-
-
Save ZiTAL/79c24c1f9248eaf26000 to your computer and use it in GitHub Desktop.
bytes converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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