Created
July 23, 2013 02:37
-
-
Save wenzhixin/6059446 to your computer and use it in GitHub Desktop.
单位转换
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
function getSize(value) { | |
var b1 = Math.pow(2, 10), //KB | |
b2 = Math.pow(2, 20), //MB | |
b3 = Math.pow(2, 30), //GB | |
b4 = Math.pow(2, 40), //TB | |
value = value.toLowerCase(), | |
size = parseFloat(value); | |
if (!size) { | |
return 0; | |
} | |
if (value.indexOf('k') !== -1) { | |
size *= b1; | |
} | |
else if (value.indexOf('m') !== -1) { | |
size *= b2; | |
} | |
else if (value.indexOf('g') !== -1) { | |
size *= b3; | |
} | |
else if (value.indexOf('t') !== -1) { | |
size *= b4; | |
} | |
return size; | |
} | |
function getSizeUnit(totalByte) { | |
var b1 = Math.pow(2, 10), | |
b2 = Math.pow(2, 20), | |
b3 = Math.pow(2, 30), | |
b4 = Math.pow(2, 40), | |
b5 = Math.pow(2, 50), | |
b6 = Math.pow(2, 60), | |
result = ''; | |
if (totalByte === 0) { | |
return '0 B'; | |
} | |
if (totalByte === Number(null)) { | |
return ''; | |
} | |
if (totalByte < b1) { | |
result = Math.ceil(totalByte) + ' B'; | |
} | |
else if (totalByte < b2) { | |
result = Math.ceil(totalByte / b1) + ' KB'; | |
} | |
else if (totalByte < b3) { | |
result = (totalByte / b2).toFixed(2) + ' MB'; | |
} | |
else if (totalByte < b4) { | |
result = (totalByte / b3).toFixed(2) + ' GB'; | |
} | |
else if (totalByte < b5) { | |
result = (totalByte / b4).toFixed(2) + ' TB'; | |
} | |
else if (totalByte < b6) { | |
result = (totalByte / b5).toFixed(2) + ' PB'; | |
} | |
else { | |
result = (totalByte / b6).toFixed(2) + ' EB';//ZB;YB;BB | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment