diff options
author | PJ Fanning <fanningpj@apache.org> | 2020-10-20 16:07:10 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2020-10-20 16:07:10 +0000 |
commit | dd9f132c232c24d939adba5065280fbba0e4ed42 (patch) | |
tree | f0d75d29cfba803c33bf8c4d4ef0b4dc322874b1 /src | |
parent | ccad1fc86e51f39141d304b7cc5980f7d50dbd7f (diff) | |
download | poi-dd9f132c232c24d939adba5065280fbba0e4ed42.tar.gz poi-dd9f132c232c24d939adba5065280fbba0e4ed42.zip |
[github-193] Change TRUNC implementation to use MathX. Thanks to Jacob Harris. This closes #193
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1882706 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r-- | src/java/org/apache/poi/ss/formula/functions/NumericFunction.java | 4 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java | 15 |
2 files changed, 16 insertions, 3 deletions
diff --git a/src/java/org/apache/poi/ss/formula/functions/NumericFunction.java b/src/java/org/apache/poi/ss/formula/functions/NumericFunction.java index 1927848555..87646295bd 100644 --- a/src/java/org/apache/poi/ss/formula/functions/NumericFunction.java +++ b/src/java/org/apache/poi/ss/formula/functions/NumericFunction.java @@ -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(); diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java b/src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java index a501435616..c8f4d90f5f 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java @@ -58,6 +58,21 @@ public final class TestTrunc extends BaseTestNumeric { } @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) }; ValueEval result = NumericFunction.TRUNC.evaluate(args, -1, (short)-1); |