Purpose:
This function returns a string containing the size of a file converted
to the most "natural" unit based on its size. It selects the size unit
where the value is between 1 and the factor to the next size unit (using a factor of 1024 (= 2^10)). Some companies may choose to use 1000 instead.
The function allows submitting numbers beyond the factor for the last unit size specified in vaUnits.
If the number of bytes is up to 1 kb, it is returned as a string with no decimals, otherwise it will return 2 decimals.
Usage:
Response.Write "<td width=""60%"">" & sFileName & "</td><td>" _
& FileSizeString(lTotalBytes) & "</td>"'
Function FileSizeString(ByVal vFileSize)
Dim vaUnits
Dim iUnitIndex
Dim iDecimals
Const iKSize = 1024
'up to 1024 PetaBytes should be OK for now...
vaUnits = Array(" Bytes", " kb", " Mb", " Gb", " Tb", " Pb")
iDecimals = 0 'If number in Bytes don't display any decimals
iUnitIndex = 0 'indicates which unit size to use.
'Find the most natural unit that has a unit description:
While vFileSize > iKSize and iUnitIndex < UBound(vaUnits)
'Reduce to next higher unit:
vFileSize = vFileSize / iKSize
iUnitIndex = iUnitIndex + 1
iDecimals = 2
Wend
FileSizeString = FormatNumber(vFileSize, iDecimals) &
vaUnits(iUnitIndex)
End Function
Submitted by Tore Bostrup