From 65048884d51ec11848d34eb708a98d3db58f8460 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 3 Feb 2022 11:14:10 +0000 Subject: [PATCH] [bug-62857] fix DOLLAR function git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897719 13f79535-47bb-0310-9956-ffa450edef68 --- .../ss/formula/functions/NumericFunction.java | 17 +++++++++++++++-- .../formula/functions/TestNumericFunction.java | 3 +-- 2 files changed, 16 insertions(+), 4 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 54369afa2e..d79627a798 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 @@ -20,8 +20,13 @@ package org.apache.poi.ss.formula.functions; import static org.apache.poi.ss.formula.eval.ErrorEval.VALUE_INVALID; import org.apache.poi.ss.formula.eval.*; +import org.apache.poi.util.LocaleUtil; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.MathContext; import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.util.Locale; @@ -93,6 +98,13 @@ public abstract class NumericFunction implements Function { return VALUE_INVALID; } + if (nPlaces < 0) { + BigDecimal divisor = BigDecimal.valueOf(Math.pow(10, -nPlaces)); + BigInteger bigInt = BigDecimal.valueOf(val).divide(divisor, MathContext.DECIMAL128) + .toBigInteger().multiply(divisor.toBigInteger()); + val = bigInt.doubleValue(); + } + StringBuilder decimalPlacesFormat = new StringBuilder(); if (nPlaces > 0) { decimalPlacesFormat.append('.'); @@ -104,10 +116,11 @@ public abstract class NumericFunction implements Function { decimalFormatString.append("$#,##0").append(decimalPlacesFormat) .append(";($#,##0").append(decimalPlacesFormat).append(')'); - DecimalFormat df = new DecimalFormat(decimalFormatString.toString()); + DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale()); + DecimalFormat df = new DecimalFormat(decimalFormatString.toString(), symbols); return new StringEval(df.format(val)); - }catch (EvaluationException e) { + } 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 35d82acd57..b5ecfd3ce9 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 @@ -59,8 +59,7 @@ final class TestNumericFunction { //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(-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"); -- 2.39.5