public static long percentile(List<Long> values, int percentile) {

    if (values.size() == 0) {
      throw new IllegalArgumentException("Percentile of empty list is not defined.");
    }

    if (percentile > 100 || percentile < 0) {
      throw new IllegalArgumentException("Percentile has to be between 0-100");
    }

    if (percentile == 0) {
      return 0;
    }

    Collections.sort(values);

    // Use Nearest Rank method.
    // https://en.wikipedia.org/wiki/Percentile#The_Nearest_Rank_method
    int position = (int) Math.ceil(values.size() * percentile / 100.0);

    // should never happen.
    if (position == 0) {
      return values.get(position);
    }

    // position is always one greater than index. Return value at the proper index
    return values.get(position - 1);
  }
The percentile method returns the least value from the given list which has at least given percentile.
@param values The list of values to find the percentile from
@param percentile The percentile
@return The least value from the list with at least the given percentile
app/com/linkedin/drelephant/math/Statistics.java