From: PJ Fanning Date: Sat, 28 May 2022 18:42:32 +0000 (+0000) Subject: [bug-66097] support CEILING.PRECISE function X-Git-Tag: REL_5_2_3~262 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=47a3a5da89b6a0bd38fa75d080279148fc517e94;p=poi.git [bug-66097] support CEILING.PRECISE function git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901367 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java index 5dc84f1462..b6830ea4fd 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java @@ -80,6 +80,7 @@ public final class AnalysisToolPak implements UDFFinder { r(m, "BIN2OCT", null); r(m, "COMPLEX", Complex.instance); r(m, "CEILING.MATH", CeilingMath.instance); + r(m, "CEILING.PRECISE", CeilingPrecise.instance); r(m, "CONCAT", TextFunction.CONCAT); r(m, "CONVERT", null); r(m, "COUNTIFS", Countifs.instance); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/CeilingPrecise.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/CeilingPrecise.java new file mode 100644 index 0000000000..5350258274 --- /dev/null +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/CeilingPrecise.java @@ -0,0 +1,72 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.formula.functions; + +import org.apache.poi.ss.formula.OperationEvaluationContext; +import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.EvaluationException; +import org.apache.poi.ss.formula.eval.NumberEval; +import org.apache.poi.ss.formula.eval.OperandResolver; +import org.apache.poi.ss.formula.eval.ValueEval; + +import java.math.RoundingMode; + +import static org.apache.poi.ss.formula.functions.MathX.scaledRoundUsingBigDecimal; + +/** + * Implementation for Excel CEILING.PRECISE() function. + * + */ +public final class CeilingPrecise implements FreeRefFunction { + + public static final CeilingPrecise instance = new CeilingPrecise(); + + private CeilingPrecise() {} + + @Override + public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { + if (args.length == 0) { + return ErrorEval.VALUE_INVALID; + } + try { + Double xval = evaluateValue(args[0], ec.getRowIndex(), ec.getColumnIndex()); + if (xval == null) { + return ErrorEval.VALUE_INVALID; + } + double multiplier = 1.0; + if (args.length > 1) { + Double arg1Val = evaluateValue(args[1], ec.getRowIndex(), ec.getColumnIndex()); + multiplier = arg1Val != null ? Math.abs(arg1Val.doubleValue()) : 1.0; + } + if (multiplier != 1.0) { + return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, RoundingMode.CEILING)); + } + return new NumberEval(Math.ceil(xval)); + } catch (EvaluationException evaluationException) { + return evaluationException.getErrorEval(); + } + } + + private static Double evaluateValue(ValueEval arg, int srcRowIndex, int srcColumnIndex) throws EvaluationException { + ValueEval veText = OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex); + String strText1 = OperandResolver.coerceValueToString(veText); + return OperandResolver.parseDouble(strText1); + } +} \ No newline at end of file diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/FloorPrecise.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/FloorPrecise.java index 83b19ea8b7..ef403a3ab5 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/FloorPrecise.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/FloorPrecise.java @@ -56,8 +56,7 @@ public final class FloorPrecise implements FreeRefFunction { multiplier = arg1Val != null ? Math.abs(arg1Val.doubleValue()) : 1.0; } if (multiplier != 1.0) { - RoundingMode mode = multiplier < 0.0 ? RoundingMode.CEILING : RoundingMode.FLOOR; - return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, mode)); + return new NumberEval(scaledRoundUsingBigDecimal(xval, multiplier, RoundingMode.FLOOR)); } return new NumberEval(Math.floor(xval)); } catch (EvaluationException evaluationException) { diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestCeilingPrecise.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestCeilingPrecise.java new file mode 100644 index 0000000000..52141477bd --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestCeilingPrecise.java @@ -0,0 +1,76 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +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.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.FormulaError; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.apache.poi.ss.util.Utils.assertDouble; +import static org.apache.poi.ss.util.Utils.assertError; + +/** + * Tests for {@link CeilingPrecise} + */ +final class TestCeilingPrecise { + + //https://support.microsoft.com/en-us/office/ceiling-precise-function-f366a774-527a-4c92-ba49-af0a196e66cb + @Test + void testMicrosoftExamples() throws IOException { + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + assertDouble(fe, cell, "CEILING.PRECISE(4.3)", 5, 0.00000000000001); + assertDouble(fe, cell, "CEILING.PRECISE(-4.3)", -4, 0.00000000000001); + assertDouble(fe, cell, "CEILING.PRECISE(4.3,2)", 6, 0.00000000000001); + assertDouble(fe, cell, "CEILING.PRECISE(4.3,-2)", 6, 0.00000000000001); + assertDouble(fe, cell, "CEILING.PRECISE(-4.3,2)", -4, 0.00000000000001); + assertDouble(fe, cell, "CEILING.PRECISE(-4.3,-2)", -4, 0.00000000000001); + } + } + + @Test + void testInvalid() throws IOException { + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + assertError(fe, cell, "CEILING.PRECISE()", FormulaError.VALUE); + } + } + + @Test + void testNumError() throws IOException { + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + assertError(fe, cell, "CEILING.PRECISE(\"abc\")", FormulaError.VALUE); + } + } +}