Created
September 2, 2016 14:47
-
-
Save anonymous/11559d8adcf03e9c58928757448d45ca to your computer and use it in GitHub Desktop.
TryCF Gist
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
<cfscript> | |
string function toHumanBytes(required numeric bytes, boolean decimal = true){ | |
var bytesAtLevel = arguments.bytes; | |
var levels = arguments.decimal ? ['B','kB','MB','GB','TB','PB', 'EB'] : ['B','KiB','MiB','GiB','TiB','PiB', 'EiB']; | |
var maxLevel = levels.Len(); | |
var step = arguments.decimal ? 1000 : 1024; | |
var currentLevel = 1; | |
while( bytesAtLevel > step && currentLevel < maxLevel ){ | |
currentLevel++; | |
bytesAtLevel = Int(bytesAtLevel / step); | |
} | |
return "#bytesAtLevel# #levels[currentLevel]#"; | |
} | |
WriteOutput(toHumanBytes(1)); | |
WriteOutput('<br>'); | |
WriteOutput(toHumanBytes(1024)); | |
WriteOutput('<br>'); | |
WriteOutput(toHumanBytes(1048576)); | |
WriteOutput('<br>'); | |
WriteOutput(toHumanBytes(1234567)); | |
WriteOutput('<br>'); | |
WriteOutput(toHumanBytes(9999999999)); | |
WriteOutput('<br>'); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment