Skip to content

percentile

Calculates the p-th percentile of an array of numbers using linear interpolation between the closest ranks. Returns NaN for an empty array. Does not mutate the input array.

Available since v3.0.3

import { percentile } from '@helpers4/array';
// or, from the all-in-one package (same code, one install):
import { percentile } from 'helpers4/array';
percentile(array: readonly number[], p: number): number
ParameterTypeDescription
arrayreadonly number[]The array of numbers
pnumberThe percentile to compute, from `0` to `100`

number — The interpolated value at percentile p, or NaN if the array is empty

The 50th percentile is equivalent to the median.

percentile([1, 2, 3, 4], 50)
// => 2.5

The 0th and 100th percentiles are the min and max.

percentile([4, 1, 3, 2], 0)   // => 1
percentile([4, 1, 3, 2], 100) // => 4

View source on GitHub