]> source.dussan.org Git - poi.git/commitdiff
Bug 51339 - Fixed arithmetic rounding in formula evaluation
authorYegor Kozlov <yegor@apache.org>
Mon, 13 Jun 2011 10:34:43 +0000 (10:34 +0000)
committerYegor Kozlov <yegor@apache.org>
Mon, 13 Jun 2011 10:34:43 +0000 (10:34 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1135079 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/ss/formula/functions/MathX.java
src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java

index 5700f1debd837e2bd80a72b14f8612e430ad4c45..087d6eb22ebb06d4ac72f70224df16034516142e 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="add">51339 - Fixed arithmetic rounding in formula evaluation </action>
            <action dev="poi-developers" type="add">51356 - Support autoSizeColumn in SXSSF</action>
            <action dev="poi-developers" type="add">51335 - Parse picture goal and crop sizes in HWPF</action>
            <action dev="poi-developers" type="add">51305 - Add sprmTCellPaddingDefault support in HWPF</action>
index 4914ad50609e7255ebc5ef3e9ad700c5c6d57f97..5696c51f81d2d764fd1e3a7bdaf6d50aa77599db 100644 (file)
@@ -53,13 +53,7 @@ final class MathX {
             retval = Double.NaN;
         }
         else {
-            if (p != 0) {
-                double temp = Math.pow(10, p);
-                retval = Math.round(n*temp)/temp;
-            }
-            else {
-                retval = Math.round(n);
-            }
+            retval = java.math.BigDecimal.valueOf(n).setScale(p, java.math.RoundingMode.HALF_UP).doubleValue();
         }
 
         return retval;
@@ -87,22 +81,7 @@ final class MathX {
             retval = Double.NaN;
         }
         else {
-            if (p != 0) {
-                double temp = Math.pow(10, p);
-                double nat = Math.abs(n*temp);
-
-                retval = sign(n) *
-                    ((nat == (long) nat)
-                            ? nat / temp
-                            : Math.round(nat + 0.5) / temp);
-            }
-            else {
-                double na = Math.abs(n);
-                retval = sign(n) *
-                    ((na == (long) na)
-                        ? na
-                        : (long) na + 1);
-            }
+            retval = java.math.BigDecimal.valueOf(n).setScale(p, java.math.RoundingMode.UP).doubleValue();
         }
 
         return retval;
@@ -130,13 +109,7 @@ final class MathX {
             retval = Double.NaN;
         }
         else {
-            if (p != 0) {
-                double temp = Math.pow(10, p);
-                retval = sign(n) * Math.round((Math.abs(n)*temp) - 0.5)/temp;
-            }
-            else {
-                retval = (long) n;
-            }
+            retval = java.math.BigDecimal.valueOf(n).setScale(p, java.math.RoundingMode.DOWN).doubleValue();
         }
 
         return retval;
index dd6974b35da2298af070b9b3af9f5a3db837fa26..1f5c4c8e841ebde2fc6b452b1711d722223991fd 100644 (file)
@@ -672,6 +672,9 @@ public class TestMathX extends AbstractNumericTestCase {
         
         d = 150.0; p = -2;
         assertEquals("round ", 200, MathX.round(d, p));
+
+        d = 2162.615d; p = 2;
+        assertEquals("round ", 2162.62d, MathX.round(d, p));
     }
 
     public void testRoundDown() {
index f8d71f94c50d6fa0b65233ca4d85a3c8e01e6f36..1542c360982505e425a20a397af70bdcde38ec66 100644 (file)
@@ -281,4 +281,23 @@ public abstract class BaseTestFormulaEvaluator extends TestCase {
         assertEquals(3.5, cellB1.getNumericCellValue(), 0.0);
     }
 
+
+    public void testRounding_bug51339() {
+        Workbook wb = _testDataProvider.createWorkbook();
+        Sheet sheet = wb.createSheet("Sheet1");
+        Row row = sheet.createRow(0);
+        Cell cellA1 = row.createCell(0);
+        cellA1.setCellValue(2162.615d);
+        Cell cellB1 = row.createCell(1);
+        cellB1.setCellFormula("round(a1,2)");
+        Cell cellC1 = row.createCell(2);
+        cellC1.setCellFormula("roundup(a1,2)");
+        Cell cellD1 = row.createCell(3);
+        cellD1.setCellFormula("rounddown(a1,2)");
+        FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
+
+        assertEquals(2162.62, fe.evaluateInCell(cellB1).getNumericCellValue(), 0.0);
+        assertEquals(2162.62, fe.evaluateInCell(cellC1).getNumericCellValue(), 0.0);
+        assertEquals(2162.61, fe.evaluateInCell(cellD1).getNumericCellValue(), 0.0);
+    }
 }
\ No newline at end of file