ColdFusion UDF Library - FormatSize
December 16th, 2008
«« Back to ColdFusion Library
Category:
SYSTEM
Description:
Converts a numeric size value to an appropriate string representation using bytes, kilobytes, megabytes, gigabytes, terabytes, or petabytes as needed
Parameters:
Returns:
String
Requires:
Sample Use:
Copy to Clipboard
Category:
SYSTEM
Description:
Converts a numeric size value to an appropriate string representation using bytes, kilobytes, megabytes, gigabytes, terabytes, or petabytes as needed
Parameters:
- sz (Numeric, required)
Value to convert - unit (String, Default: B)
Unit used for sz. - format (String, Default: ,.00)
A format specifier as used byu the built-in NumberFormat function
Returns:
String
Requires:
Sample Use:
Copy to Clipboard
function FormatSize(sz) {
var result = "";
var validUnits = "B,KB,MB,GB,TB,PB";
var unitIdx = 1;
var unit = "B";
var format = ",.00";
if (arrayLen(arguments) GT 1) {
unit = arguments[2];
unitIdx = listFindNoCase(validUnits, unit);
if (arrayLen(arguments) GT 2) {
format = arguments[3];
}
}
if (unitIdx GT 0) {
while ((sz GTE 1024) AND (unitIdx LT listLen(validUnits))) {
sz = (sz / 1024);
unitIdx = unitIdx + 1;
}
result = numberFormat(sz, format) & " " & listGetAt(validUnits, unitIdx);
}
else {
result = "INVALID UNIT";
}
return result;
}
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=2&function=FormatSize



