diff options
author | PJ Fanning <fanningpj@apache.org> | 2022-02-03 15:26:48 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2022-02-03 15:26:48 +0000 |
commit | 77bcd7759067dbc1baa058ccafe762d86dcbcbbb (patch) | |
tree | 27c08d9d349c8055c5b181c784bdd7f4e7b6763c /poi | |
parent | 2b646f37be2d1673f77d3b01d1ec3c356c8702b7 (diff) | |
download | poi-77bcd7759067dbc1baa058ccafe762d86dcbcbbb.tar.gz poi-77bcd7759067dbc1baa058ccafe762d86dcbcbbb.zip |
[bug-62857] support locale
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897729 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r-- | poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java | 12 | ||||
-rw-r--r-- | poi/src/test/java/org/apache/poi/ss/formula/functions/TestNumericFunction.java | 8 |
2 files changed, 17 insertions, 3 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java index e58a47c63c..3a3bc4760c 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java @@ -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(); } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNumericFunction.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNumericFunction.java index cd6071bad7..a5035c0924 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNumericFunction.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNumericFunction.java @@ -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); } |