mirror of https://github.com/apache/poi.git
[bug-62857] fix DOLLAR function
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897718 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
98a93d1041
commit
4690765436
|
@ -21,6 +21,10 @@ import static org.apache.poi.ss.formula.eval.ErrorEval.VALUE_INVALID;
|
||||||
|
|
||||||
import org.apache.poi.ss.formula.eval.*;
|
import org.apache.poi.ss.formula.eval.*;
|
||||||
|
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.text.NumberFormat;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
public abstract class NumericFunction implements Function {
|
public abstract class NumericFunction implements Function {
|
||||||
|
|
||||||
private static final double ZERO = 0.0;
|
private static final double ZERO = 0.0;
|
||||||
|
@ -89,10 +93,20 @@ public abstract class NumericFunction implements Function {
|
||||||
return VALUE_INVALID;
|
return VALUE_INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO - DOLLAR() function impl is NQR
|
StringBuilder decimalPlacesFormat = new StringBuilder();
|
||||||
// result should be StringEval, with leading '$' and thousands separators
|
if (nPlaces > 0) {
|
||||||
// current junits are asserting incorrect behaviour
|
decimalPlacesFormat.append('.');
|
||||||
return new NumberEval(val);
|
}
|
||||||
|
for (int i = 0; i < nPlaces; i++) {
|
||||||
|
decimalPlacesFormat.append('0');
|
||||||
|
}
|
||||||
|
StringBuilder decimalFormatString = new StringBuilder();
|
||||||
|
decimalFormatString.append("$#,##0").append(decimalPlacesFormat)
|
||||||
|
.append(";($#,##0").append(decimalPlacesFormat).append(')');
|
||||||
|
|
||||||
|
DecimalFormat df = new DecimalFormat(decimalFormatString.toString());
|
||||||
|
|
||||||
|
return new StringEval(df.format(val));
|
||||||
}catch (EvaluationException e) {
|
}catch (EvaluationException e) {
|
||||||
return e.getErrorEval();
|
return e.getErrorEval();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.apache.poi.ss.util.Utils.assertDouble;
|
import static org.apache.poi.ss.util.Utils.assertDouble;
|
||||||
|
import static org.apache.poi.ss.util.Utils.assertString;
|
||||||
|
|
||||||
final class TestNumericFunction {
|
final class TestNumericFunction {
|
||||||
|
|
||||||
|
@ -50,4 +51,18 @@ final class TestNumericFunction {
|
||||||
assertDouble(fe, cell, "SIGN(-0.00001)", -1.0, 0);
|
assertDouble(fe, cell, "SIGN(-0.00001)", -1.0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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)");
|
||||||
|
//TODO need to fix code to handle next case
|
||||||
|
//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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue