ColdFusion UDF Library - FileSize
December 16th, 2008
«« Back to ColdFusion Library
Category:
SYSTEM
Description:
Formats file size to specified units like KB, MB, etc.
Parameters:
Returns:
String
Requires:
PrecisRound()
Sample Use:
Copy to Clipboard
Category:
SYSTEM
Description:
Formats file size to specified units like KB, MB, etc.
Parameters:
- size (Numeric, required)
The size of the file - precision (Numeric, Default: 2)
Rounds size to the specificed number of decimal places - unit (String, Default: AUTO)
Unit used for size
Returns:
String
Requires:
PrecisRound()
Sample Use:
Copy to Clipboard
/*#depends:preciseRound#*/
function FileSize(size){
var units = "kb,mb,gb,tb,pb,eb,zb,yb";
var unit = "";
var pos = 0;
var value = "";
var counter = 0;
var precision = 2;
if (arrayLen(arguments) gte 2)precision = arguments[2];
if (arrayLen(arguments) gte 3)unit = arguments[3];
if (len(unit)){
if (isNumeric(unit))pos = unit;
else{
if (listFindNoCase(units,unit))pos = listFindNoCase(units,unit);
else unit = "";
};
}
if (not pos){
while (size/(1024^counter) gt 1)counter = incrementValue(counter);
pos = counter-1;
}
if (pos){
size = server.preciseRound(size/(1024^pos),precision);
value = "#numberFormat(size,",#iif(findNoCase(".",size),de(".#repeatString("9",precision)#"),de(""))#")# #uCase(listGetAt(units,pos))#";
}
else value = size;
return value;
}
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=3&function=FileSize



