averageWithoutZeros(data)
Last updated January 12, 2004
Version: 1 | Requires: CF5 | Library: MathLib
Description:
ColdFusion's built-in arrayAvg() function calculates an average by taking the sum of a set of numbers and then dividing that sum by the total count of the numbers. When the set of numbers contains one or more zeros, this produces an inaccurate average when the developer wishes to average only those values which are greater than or equal to 1.
Return Values:
Returns a number.
Example:
<cfset data = listToArray("5,0,5,3,0")>
<cfoutput>
Built-in ArrayAvg: #arrayAvg(data)#<br>
AverageWithoutZeros: #averageWithoutZeros(data)#
</cfoutput>
Parameters:
Name | Description | Required |
---|---|---|
data | The array to average. | Yes |
Full UDF Source:
/**
* Calculates the average of a set of numbers omitting values less than 1 from that average.
*
* @param data The array to average. (Required)
* @return Returns a number.
* @author David Simms (dsimms@dcbar.org)
* @version 1, January 12, 2004
*/
function averageWithoutZeros(data) {
var counter = arrayLen(data);
//remove zeros
for(;counter gte 1;counter=counter-1) {
if(data[counter] lt 1) arrayDeleteAt(data,counter);
}
if(arrayLen(data)) return arrayAvg(data);
else return 0;
}
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