Calculate Percentile

Calculate the nth percentile of an array using linear interpolation.

Code

General
sorted = arr.sort
index = (p / 100.0) * (sorted.length - 1)
lower = index.floor
fraction = index - lower
result = sorted[lower] + fraction * ((sorted[lower + 1] || sorted[lower]) - sorted[lower])
return result

Parameters

Array of numbers

Percentile (0-100)

Server

More Ruby Snippets