Skip to content

percentile

计算数组的百分位数

数字输入

百分位设置

P50 结果

5.50

函数签名

typescript
function percentile(numbers: number[], p: number): number

参数

参数名类型必填说明
numbersnumber[]数字数组
pnumber百分位(0-100)

返回值

类型说明
number指定百分位的数值

异常

错误类型触发条件错误信息
Error数组为空'Array cannot be empty'
Errorp不在0-100范围内'Percentile must be between 0 and 100'

工作原理

  1. 验证数组不为空,验证p在0-100范围内
  2. 对数组进行升序排序
  3. 计算百分位对应的索引位置
  4. 如果索引为整数,直接返回该位置的值
  5. 如果索引为小数,使用线性插值计算两个相邻值之间的结果
  6. 返回百分位数值