CFLib.org – Common Function Library Project

KByte(bytes)

Last updated August 01, 2005

author

John Bartlett

Version: 1 | Requires: CF6 | Library: UtilityLib

Description:
Given a number of bytes, this function will return an easier to read formatted value. For example, "10" would return "10 bytes", "1024" would return "1 KB", "1560" would return "1.5 KB".

Return Values:
Returns a string.

Example:

<cfoutput>
1023: #KBytes(1023)#<br>
1024: #KBytes(1024)#<br>
1560: #KBytes(1560)#<br>
1482843: #KBytes(1482843)#
</cfoutput>

Parameters:

Name Description Required
bytes The number of bytes. Yes

Full UDF Source:

/**
 * Converts a byte value into kb or mb if over 1,204 bytes.
 * 
 * @param bytes      The number of bytes. (Required)
 * @return Returns a string. 
 * @author John Bartlett (jbartlett@strangejourney.net) 
 * @version 1, July 31, 2005 
 */
function KBytes(bytes) {
    var b=0;

    if(arguments.bytes lt 1024) return trim(numberFormat(arguments.bytes,"9,999")) & " bytes";
    
    b=arguments.bytes / 1024;
    
    if (b lt 1024) {
        if(b eq int(b)) return trim(numberFormat(b,"9,999")) & " KB";
        return trim(numberFormat(b,"9,999.9")) & " KB";
    }
    b= b / 1024;
    if (b eq int(b)) return trim(numberFormat(b,"999,999,999")) & " MB";
    return trim(numberFormat(b,"999,999,999.9")) & " MB";
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson