percentile static method

num percentile(
  1. List<num> values,
  2. double percentile
)

Calculates the specified percentile of a list of values.

percentile must be between 0 and 100 inclusive.

Implementation

static num percentile(List<num> values, double percentile) {
  if (values.isEmpty) throw ArgumentError('The list cannot be empty.');
  if (percentile < 0 || percentile > 100) {
    throw ArgumentError('Percentile must be between 0 and 100.');
  }
  final sortedValues = List<num>.from(values)..sort();
  final rank = percentile / 100;
  final index = (rank * (values.length - 1)).round();
  return sortedValues[index];
}