[bug-62857] support locale

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897729 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-02-03 15:26:48 +00:00
parent 2b646f37be
commit 77bcd77590
2 changed files with 17 additions and 3 deletions

View 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();
}

View 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);
}