CFLib.org – Common Function Library Project

nthPercentile(population, percentile)

Last updated July 08, 2013

Version: 1 | Requires: CF9 | Library: CFMLLib

Description:
Reference: http://en.wikipedia.org/wiki/95th_percentile

Return Values:
Returns a numeric value that is the value from the population that is the specified percentile

Example:

``````<!---get the 90th %tile--->
<cfset pop = [1,2,6,7,3,5,6,8,0,2,3,4,5,9,12,23,78]>

<cfset nthPercentile = getNthPercentile(pop,90)>

<cfoutput>#nthPercentile#</cfoutput>
``````

Parameters:

Name Description Required
population An array of population values Yes
percentile The percentile to return value for Yes

Full UDF Source:

``````/**
* Gets the nth percentile of the given population
* v0.9 by rodell basalo
* v1.0 by Adam Cameron (improved argument/variable naming, simplified logic slightly)
*
* @param population      An array of population values (Required)
* @param percentile      The percentile to return value for (Required)
* @return Returns a numeric value that is the value from the population that is the specified percentile
* @author rodell basalo (delroekid@gmail.com)
* @version 1.0, July 8, 2013
*/
public numeric function getNthPercentile(required array population, required numeric percentile){
if (percentile < 0 || percentile > 100){
throw(type="ArgumentOutOfRangeException", message="percentile argument value out of range", detail="The percentile argument must be in the range 1-100")
}
arraySort(population, "numeric");

var populationSize = arrayLen(population);
var nthPercentIndex = round((percentile/100) * populationSize + 0.5);

return population[min(nthPercentIndex, populationSize)];
}
``````

Search CFLib.org

QueryDeleteRows
November 04, 2017

May 11, 2016