[bug-62857] support locale

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897723 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-02-03 12:24:59 +00:00
parent 65048884d5
commit d8b574b325
3 changed files with 64 additions and 14 deletions

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

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

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