if (d < x) lessThanCount++;
}
BigDecimal result = new BigDecimal((double)(lessThanCount + 1) / (double)(numbers.size() + 1));
- return new NumberEval(PercentRank.round(result, significance, RoundingMode.DOWN));
+ return new NumberEval(PercentRank.round(result, significance));
} else {
- ValueEval belowRank = calculateRank(numbers, closestMatchBelow, significance, false);
+ ValueEval belowRank = calculateRank(numbers, closestMatchBelow, significance + 3, false);
if (!(belowRank instanceof NumberEval)) {
return belowRank;
}
- ValueEval aboveRank = calculateRank(numbers, closestMatchAbove, significance, false);
+ ValueEval aboveRank = calculateRank(numbers, closestMatchAbove, significance + 3, false);
if (!(aboveRank instanceof NumberEval)) {
return aboveRank;
}
package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.eval.*;
+import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.util.Internal;
import java.math.BigDecimal;
return ErrorEval.NA;
}
BigDecimal result = new BigDecimal((double)lessThanCount / (double)(lessThanCount + greaterThanCount));
- return new NumberEval(round(result, significance, RoundingMode.DOWN));
+ return new NumberEval(round(result, significance));
} else {
- ValueEval belowRank = calculateRank(numbers, closestMatchBelow, significance, false);
+ ValueEval belowRank = calculateRank(numbers, closestMatchBelow, significance + 3, false);
if (!(belowRank instanceof NumberEval)) {
return belowRank;
}
- ValueEval aboveRank = calculateRank(numbers, closestMatchAbove, significance, false);
+ ValueEval aboveRank = calculateRank(numbers, closestMatchAbove, significance + 3, false);
if (!(aboveRank instanceof NumberEval)) {
return aboveRank;
}
NumberEval belowRank, NumberEval aboveRank, int significance) {
double diff = closestMatchAbove - closestMatchBelow;
double pos = x - closestMatchBelow;
- double rankDiff = aboveRank.getNumberValue() - belowRank.getNumberValue();
- BigDecimal result = new BigDecimal(belowRank.getNumberValue() + (rankDiff * (pos / diff)));
- return new NumberEval(round(result, significance, RoundingMode.HALF_UP));
+ BigDecimal rankDiff = new BigDecimal(NumberToTextConverter.toText(aboveRank.getNumberValue() - belowRank.getNumberValue()));
+ BigDecimal result = new BigDecimal(belowRank.getNumberValue()).add(rankDiff.multiply(new BigDecimal(pos / diff)));
+ return new NumberEval(round(result, significance));
}
@Internal
- public static double round(BigDecimal bd, int significance, RoundingMode rounding) {
+ public static double round(BigDecimal bd, int significance) {
//the rounding in https://support.microsoft.com/en-us/office/percentrank-function-f1b5836c-9619-4847-9fc9-080ec9024442
//is very inconsistent, this hodge podge of rounding modes is the only way to match Excel results
- return bd.setScale(significance, rounding).doubleValue();
+ BigDecimal bd2 = bd.setScale(significance + 3, RoundingMode.HALF_UP);
+ return bd2.setScale(significance, RoundingMode.DOWN).doubleValue();
}
@Internal
// PERCENTRANK.INC test case (for comparison)
@Test
- void testMicrosoftExample1() throws IOException {
+ void testPercentRankIncExample1() throws IOException {
try (HSSFWorkbook wb = initWorkbook1()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
}
}
+ //https://support.microsoft.com/en-us/office/percentrank-exc-function-d8afee96-b7e2-4a2f-8c01-8fcdedaa6314
+ @Test
+ void testMicrosoftExample1() throws IOException {
+ try (HSSFWorkbook wb = initWorkbook2()) {
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
+ assertDouble(fe, cell, "PERCENTRANK.EXC(A2:A10, 7)", 0.7);
+ assertDouble(fe, cell, "PERCENTRANK.EXC(A2:A10,5.43)", 0.381);
+ assertDouble(fe, cell, "PERCENTRANK.EXC(A2:A10,5.43,1)", 0.3);
+ }
+ }
+
@Test
void testErrorCases() throws IOException {
try (HSSFWorkbook wb = initWorkbook1()) {
return wb;
}
+ private HSSFWorkbook initWorkbook2() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ addRow(sheet, 0, "Data");
+ addRow(sheet, 1, 1);
+ addRow(sheet, 2, 2);
+ addRow(sheet, 3, 3);
+ addRow(sheet, 4, 6);
+ addRow(sheet, 5, 6);
+ addRow(sheet, 6, 6);
+ addRow(sheet, 7, 7);
+ addRow(sheet, 8, 8);
+ addRow(sheet, 9, 9);
+ return wb;
+ }
+
private static void confirmErrorResult(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText, FormulaError expectedResult) {
cell.setCellFormula(formulaText);
fe.notifyUpdateCell(cell);
assertDouble(fe, cell, "PERCENTRANK.INC(A2:A11,8,2)", 0.66);
assertDouble(fe, cell, "PERCENTRANK.INC(A2:A11,8,4)", 0.6666);
assertDouble(fe, cell, "PERCENTRANK.INC(A2:A11,5)", 0.583);
+ assertDouble(fe, cell, "PERCENTRANK.INC(A2:A11,5,5)", 0.58333);
assertDouble(fe, cell, "PERCENTRANK.INC(A2:A11,1)", 0);
assertDouble(fe, cell, "PERCENTRANK.INC(A2:A11,13)", 1);
}