]> source.dussan.org Git - poi.git/commitdiff
[github-193] Change TRUNC implementation to use MathX. Thanks to Jacob Harris. This...
authorPJ Fanning <fanningpj@apache.org>
Tue, 20 Oct 2020 16:07:10 +0000 (16:07 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 20 Oct 2020 16:07:10 +0000 (16:07 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1882706 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/functions/NumericFunction.java
src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java

index 19278485559d44481903c605f124965056d67bf3..87646295bde302ae0135c90cd0867bfc06d9bcc3 100644 (file)
@@ -330,9 +330,7 @@ public abstract class NumericFunction implements Function {
                        try {
                                double d0 = singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
                                double d1 = singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
-                               double multi = Math.pow(10d,d1);
-                               if(d0 < 0) result = -Math.floor(-d0 * multi) / multi;
-                else result = Math.floor(d0 * multi) / multi;
+                               result = MathX.roundDown(d0, (int)d1);
                                checkValue(result);
                        }catch (EvaluationException e) {
                                return e.getErrorEval();
index a501435616429cd7c0d4ed72ffa317bbc501b8c0..c8f4d90f5ff3f29c7b4b81fb7a479ccb60107ac7 100644 (file)
@@ -57,6 +57,21 @@ public final class TestTrunc extends BaseTestNumeric {
                assertEquals("TRUNC", (new NumberEval(2.612d)).getNumberValue(), ((NumberEval)result).getNumberValue());
        }
 
+       @Test
+       public void testTruncWithProblematicDecimalNumber() {
+               ValueEval[] args = { new NumberEval(0.29), new NumberEval(2) };
+               ValueEval result = NumericFunction.TRUNC.evaluate(args, -1, (short)-1);
+               assertEquals("TRUNC", (new NumberEval(0.29d)).getNumberValue(), ((NumberEval)result).getNumberValue());
+       }
+
+       @Test
+       public void testTruncWithProblematicCalculationResult() {
+
+               ValueEval[] args = { new NumberEval(21.624d / 24d + .009d), new NumberEval(2) };
+               ValueEval result = NumericFunction.TRUNC.evaluate(args, -1, (short)-1);
+               assertEquals("TRUNC", (new NumberEval(0.91d)).getNumberValue(), ((NumberEval)result).getNumberValue());
+       }
+
        @Test
        public void testTruncWithDecimalNumberOneArg() {
                ValueEval[] args = { new NumberEval(2.612777) };