import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.functions.NumericFunction;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
/**
* Implementation of Excel 'Analysis ToolPak' function MROUND()<br>
*
// Returns #NUM! because the number and the multiple have different signs
throw new EvaluationException(ErrorEval.NUM_ERROR);
}
- result = multiple * Math.round( number / multiple );
+ BigDecimal bdMultiple = BigDecimal.valueOf(multiple);
+ result = bdMultiple.multiply(BigDecimal.valueOf(number).divide(bdMultiple, 0, RoundingMode.HALF_UP))
+ .doubleValue();
+
}
NumericFunction.checkValue(result);
return new NumberEval(result);
*/
class TestMRound {
+ //examples from https://support.microsoft.com/en-us/office/mround-function-c299c3b0-15a5-426d-aa4b-d2d5b3baf427
/**
=MROUND(10, 3) Rounds 10 to a nearest multiple of 3 (9)
=MROUND(-10, -3) Rounds -10 to a nearest multiple of -3 (-9)
cell4.setCellFormula("MROUND(5, -2)");
Cell cell5 = sh.createRow(0).createCell(0);
cell5.setCellFormula("MROUND(5, 0)");
+ Cell cell6 = sh.createRow(0).createCell(0);
+ cell6.setCellFormula("MROUND(0.79*7.5, 0.05)");
double accuracy = 1E-9;
assertEquals(0.0, evaluator.evaluate(cell5).getNumberValue(), 0,
"Returns 0 because the multiple is 0");
+
+ assertEquals(5.95, evaluator.evaluate(cell6).getNumberValue(), 0,
+ "Rounds 5.925 to a nearest multiple of 0.05 (5.95)");
}
}