Skip to content

Instantly share code, notes, and snippets.

Created September 2, 2016 14:47
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 anonymous/11559d8adcf03e9c58928757448d45ca to your computer and use it in GitHub Desktop.
Save anonymous/11559d8adcf03e9c58928757448d45ca to your computer and use it in GitHub Desktop.
TryCF Gist
<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