diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/FloorMath.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/FloorMath.java index 0b0d13928b..71376f4cb1 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/FloorMath.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/FloorMath.java @@ -62,12 +62,14 @@ public final class FloorMath implements FreeRefFunction { } if (roundNegativeNumsDown && xval < 0.0) { if (multiplier != 1.0) { - return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, RoundingMode.CEILING)); + RoundingMode mode = multiplier < 0.0 ? RoundingMode.FLOOR : RoundingMode.CEILING; + return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, mode)); } return new NumberEval(Math.ceil(xval)); } if (multiplier != 1.0) { - return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, RoundingMode.FLOOR)); + RoundingMode mode = multiplier < 0.0 ? RoundingMode.CEILING : RoundingMode.FLOOR; + return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, mode)); } return new NumberEval(Math.floor(xval)); } catch (EvaluationException evaluationException) { diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestFloorMath.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestFloorMath.java index b171261969..8973fe7146 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestFloorMath.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestFloorMath.java @@ -47,6 +47,10 @@ final class TestFloorMath { assertDouble(fe, cell, "FLOOR.MATH(6.7)", 6.0, 0.00000000000001); assertDouble(fe, cell, "FLOOR.MATH(-8.1,2)", -10.0, 0.00000000000001); assertDouble(fe, cell, "FLOOR.MATH(-5.5,2,-1)", -4.0, 0.00000000000001); + + assertDouble(fe, cell, "FLOOR.MATH(-2.5,-2)", -4.0, 0.00000000000001); + assertDouble(fe, cell, "FLOOR.MATH(-2.5,-2,-1)", -2.0, 0.00000000000001); + assertDouble(fe, cell, "FLOOR.MATH(2.5,-2)", 2.0, 0.00000000000001); } }