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

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 e58a47c63c9a7911b7ad300f358dc3610c882377..3a3bc4760c1016e00b1f43c27b8edd3321f97558 100644 (file)
@@ -27,6 +27,7 @@ import java.math.BigInteger;
 import java.math.MathContext;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
 
 public abstract class NumericFunction implements Function {
 
@@ -117,7 +118,16 @@ public abstract class NumericFunction implements Function {
             DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
             DecimalFormat df = new DecimalFormat(decimalFormatString.toString(), symbols);
 
-            return new StringEval(df.format(val));
+            DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance(LocaleUtil.getUserLocale());
+            int decimalPlaces = nPlaces < 0 ? 0 : nPlaces;
+            if (LocaleUtil.getUserLocale().getCountry().equalsIgnoreCase("US")) {
+                nf.setNegativePrefix("(" + nf.getDecimalFormatSymbols().getCurrencySymbol());
+                nf.setNegativeSuffix(")");
+            }
+            nf.setMinimumFractionDigits(decimalPlaces);
+            nf.setMaximumFractionDigits(decimalPlaces);
+
+            return new StringEval(nf.format(val).replace("\u00a0"," "));
         } catch (EvaluationException e) {
             return e.getErrorEval();
         }
index cd6071bad7eda36e8aa62e0da2155ee268c1f486..a5035c09245589fb8cf1452bddc5e583e4bdcbe0 100644 (file)
@@ -83,6 +83,7 @@ final class TestNumericFunction {
             HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
             HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
             assertString(fe, cell, "DOLLAR(1234.567,2)", "€1,234.57");
+            assertString(fe, cell, "DOLLAR(-1234.567,2)", "-€1,234.57");
         } finally {
             LocaleUtil.setUserLocale(defaultLocale);
         }
@@ -96,7 +97,8 @@ final class TestNumericFunction {
             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");
+            assertString(fe, cell, "DOLLAR(1234.567,2)", "1.234,57 €");
+            assertString(fe, cell, "DOLLAR(-1234.567,2)", "-1.234,57 €");
         } finally {
             LocaleUtil.setUserLocale(defaultLocale);
         }
@@ -111,6 +113,7 @@ final class TestNumericFunction {
             HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
             HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
             assertString(fe, cell, "DOLLAR(1234.567,2)", "¥1,234.57");
+            assertString(fe, cell, "DOLLAR(-1234.567,2)", "-¥1,234.57");
         } finally {
             LocaleUtil.setUserLocale(defaultLocale);
         }
@@ -124,7 +127,8 @@ final class TestNumericFunction {
             HSSFWorkbook wb = new HSSFWorkbook();
             HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
             HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
-            assertString(fe, cell, "DOLLAR(1234.567,2)", "kr.1,234.57");
+            assertString(fe, cell, "DOLLAR(1234.567,2)", "1.234,57 kr.");
+            assertString(fe, cell, "DOLLAR(-1234.567,2)", "-1.234,57 kr.");
         } finally {
             LocaleUtil.setUserLocale(defaultLocale);
         }