]> source.dussan.org Git - poi.git/commitdiff
[bug-62857] support locale
authorPJ Fanning <fanningpj@apache.org>
Thu, 3 Feb 2022 12:24:59 +0000 (12:24 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 3 Feb 2022 12:24:59 +0000 (12:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897723 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java
poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java
poi/src/test/java/org/apache/poi/ss/formula/functions/TestNumericFunction.java

index b0aa9310527361c1ae5d828fbcfb18aad2857bef..c471832703bcc911ca30759f71327c564d78c2a6 100644 (file)
@@ -180,6 +180,7 @@ public final class TestFormulaEvaluatorOnXSSF {
             Cell c = formulasRow.getCell(colnum);
             assumeTrue(c != null);
             assumeTrue(c.getCellType() == CellType.FORMULA);
+            assumeFalse(targetFunctionName.equalsIgnoreCase("DOLLAR"));
             ignoredFormulaTestCase(c.getCellFormula());
 
             CellValue actValue = evaluator.evaluate(c);
index d79627a79820ccb8a4d1c95552c659722f2ced1b..e58a47c63c9a7911b7ad300f358dc3610c882377 100644 (file)
@@ -27,8 +27,6 @@ import java.math.BigInteger;
 import java.math.MathContext;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
-import java.text.NumberFormat;
-import java.util.Locale;
 
 public abstract class NumericFunction implements Function {
 
@@ -113,8 +111,8 @@ public abstract class NumericFunction implements Function {
                 decimalPlacesFormat.append('0');
             }
             StringBuilder decimalFormatString = new StringBuilder();
-            decimalFormatString.append("$#,##0").append(decimalPlacesFormat)
-                    .append(";($#,##0").append(decimalPlacesFormat).append(')');
+            decimalFormatString.append("¤#,##0").append(decimalPlacesFormat)
+                    .append(";(¤#,##0").append(decimalPlacesFormat).append(')');
 
             DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
             DecimalFormat df = new DecimalFormat(decimalFormatString.toString(), symbols);
index b5ecfd3ce9986469228790ca43a6c83ce81289e4..6f86b131cd1624ae6506691516466a36bf0352fa 100644 (file)
@@ -19,8 +19,11 @@ package org.apache.poi.ss.formula.functions;
 import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.util.LocaleUtil;
 import org.junit.jupiter.api.Test;
 
+import java.util.Locale;
+
 import static org.apache.poi.ss.util.Utils.assertDouble;
 import static org.apache.poi.ss.util.Utils.assertString;
 
@@ -53,15 +56,63 @@ final class TestNumericFunction {
 
     @Test
     void testDOLLAR() {
-        HSSFWorkbook wb = new HSSFWorkbook();
-        HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
-        HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
-        //https://support.microsoft.com/en-us/office/dollar-function-a6cd05d9-9740-4ad3-a469-8109d18ff611
-        assertString(fe, cell, "DOLLAR(1234.567,2)", "$1,234.57");
-        assertString(fe, cell, "DOLLAR(-1234.567,0)", "($1,235)");
-        assertString(fe, cell, "DOLLAR(-1234.567,-2)", "($1,200)");
-        assertString(fe, cell, "DOLLAR(-0.123,4)", "($0.1230)");
-        assertString(fe, cell, "DOLLAR(99.888)", "$99.89");
-        assertString(fe, cell, "DOLLAR(123456789.567,2)", "$123,456,789.57");
+        Locale defaultLocale = LocaleUtil.getUserLocale();
+        try {
+            LocaleUtil.setUserLocale(new Locale("en", "US"));
+            HSSFWorkbook wb = new HSSFWorkbook();
+            HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+            //https://support.microsoft.com/en-us/office/dollar-function-a6cd05d9-9740-4ad3-a469-8109d18ff611
+            assertString(fe, cell, "DOLLAR(1234.567,2)", "$1,234.57");
+            assertString(fe, cell, "DOLLAR(-1234.567,0)", "($1,235)");
+            assertString(fe, cell, "DOLLAR(-1234.567,-2)", "($1,200)");
+            assertString(fe, cell, "DOLLAR(-0.123,4)", "($0.1230)");
+            assertString(fe, cell, "DOLLAR(99.888)", "$99.89");
+            assertString(fe, cell, "DOLLAR(123456789.567,2)", "$123,456,789.57");
+        } finally {
+            LocaleUtil.setUserLocale(defaultLocale);
+        }
+    }
+
+    @Test
+    void testDOLLARIreland() {
+        Locale defaultLocale = LocaleUtil.getUserLocale();
+        try {
+            LocaleUtil.setUserLocale(new Locale("en", "IE"));
+            HSSFWorkbook wb = new HSSFWorkbook();
+            HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+            assertString(fe, cell, "DOLLAR(1234.567,2)", "€1,234.57");
+        } finally {
+            LocaleUtil.setUserLocale(defaultLocale);
+        }
+    }
+
+    @Test
+    void testDOLLARSpain() {
+        Locale defaultLocale = LocaleUtil.getUserLocale();
+        try {
+            LocaleUtil.setUserLocale(new Locale("es", "ES"));
+            HSSFWorkbook wb = new HSSFWorkbook();
+            HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+            assertString(fe, cell, "DOLLAR(1234.567,2)", "€1.234,57");
+        } finally {
+            LocaleUtil.setUserLocale(defaultLocale);
+        }
+    }
+
+    @Test
+    void testDOLLARJapan() {
+        Locale defaultLocale = LocaleUtil.getUserLocale();
+        try {
+            LocaleUtil.setUserLocale(new Locale("ja", "JP"));
+            HSSFWorkbook wb = new HSSFWorkbook();
+            HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
+            HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+            assertString(fe, cell, "DOLLAR(1234.567,2)", "¥1,234.57");
+        } finally {
+            LocaleUtil.setUserLocale(defaultLocale);
+        }
     }
 }