The ColdFusion version of the Typescript function to Typescript - Human Readable File Sizes
This stupid simple function takes bytes (as a numeric), and appends 'B', 'KB', 'MB', or 'GB' as appropriate.
// region formatBytes
/**
* given bytes, return a human readable string
* @hint formats bytes into B, KB, MB, GBs
* @bytes number of bytes
* @return string formatted as B, KB, MB, GB
* @CFLintIgnore ARG_TYPE_ANY
*/
string function formatBytes(required any bytes) {
if (NOT isValid('numeric', arguments.bytes) OR arguments.bytes LE 0) {
return '0 B';
}
var sizes = ['B', 'KB', 'MB', 'GB']
var lScale = floor(log(arguments.bytes) / log(1024));
var result = arguments.bytes / (1024 ^ lScale);
if (result LT 10) {
return '#numberFormat(result, '0.9')# #sizes[lScale + 1]#';
} else {
return '#round(result)# #sizes[lScale + 1]#';
}
}
// endregion
This is included in our common_utils
module available in all 5.5 or newer apps (not available in older apps).
Usage
The file size is #formatBytes(101364353)#
Outputs '97 MB'.