CFLib.org – Common Function Library Project

KByte(bytes)

Last updated July 31, 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";
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson