Search in sources :

Example 56 with NumberEval

use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.

the class TestMid method testExtremes.

/**
	 * Extreme values for startPos and numChars
	 */
public void testExtremes() {
    confirmMid(new StringEval("galactic"), new NumberEval(4), new NumberEval(400), "actic");
    confirmMid(new StringEval("galactic"), new NumberEval(30), new NumberEval(4), "");
    confirmMid(new StringEval("galactic"), new NumberEval(3), new NumberEval(0), "");
}
Also used : StringEval(org.apache.poi.ss.formula.eval.StringEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 57 with NumberEval

use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.

the class TestMatch method testSimpleNumber.

public void testSimpleNumber() {
    ValueEval[] values = { new NumberEval(4), new NumberEval(5), new NumberEval(10), new NumberEval(10), new NumberEval(25) };
    AreaEval ae = EvalFactory.createAreaEval("A1:A5", values);
    confirmInt(2, invokeMatch(new NumberEval(5), ae, MATCH_LARGEST_LTE));
    confirmInt(2, invokeMatch(new NumberEval(5), ae));
    confirmInt(2, invokeMatch(new NumberEval(5), ae, MATCH_EXACT));
    confirmInt(4, invokeMatch(new NumberEval(10), ae, MATCH_LARGEST_LTE));
    confirmInt(3, invokeMatch(new NumberEval(10), ae, MATCH_EXACT));
    confirmInt(4, invokeMatch(new NumberEval(20), ae, MATCH_LARGEST_LTE));
    assertEquals(ErrorEval.NA, invokeMatch(new NumberEval(20), ae, MATCH_EXACT));
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 58 with NumberEval

use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.

the class TestEDate method testEDateDecrease.

@Test
public void testEDateDecrease() {
    EDate eDate = new EDate();
    Date startDate = new Date();
    int offset = -2;
    NumberEval result = (NumberEval) eDate.evaluate(new ValueEval[] { new NumberEval(DateUtil.getExcelDate(startDate)), new NumberEval(offset) }, null);
    Date resultDate = DateUtil.getJavaDate(result.getNumberValue());
    Calendar instance = LocaleUtil.getLocaleCalendar();
    instance.setTime(startDate);
    instance.add(Calendar.MONTH, offset);
    assertEquals(resultDate, instance.getTime());
}
Also used : Calendar(java.util.Calendar) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) Date(java.util.Date) NumberEval(org.apache.poi.ss.formula.eval.NumberEval) Test(org.junit.Test)

Example 59 with NumberEval

use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.

the class CalculateMortgage method evaluate.

@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    // verify that we have enough data
    if (args.length != 3) {
        return ErrorEval.VALUE_INVALID;
    }
    // declare doubles for values
    double principal, rate, years, result;
    try {
        // extract values as ValueEval
        ValueEval v1 = OperandResolver.getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex());
        ValueEval v2 = OperandResolver.getSingleValue(args[1], ec.getRowIndex(), ec.getColumnIndex());
        ValueEval v3 = OperandResolver.getSingleValue(args[2], ec.getRowIndex(), ec.getColumnIndex());
        // get data as doubles
        principal = OperandResolver.coerceValueToDouble(v1);
        rate = OperandResolver.coerceValueToDouble(v2);
        years = OperandResolver.coerceValueToDouble(v3);
        result = calculateMortgagePayment(principal, rate, years);
        System.out.println("Result = " + result);
        checkValue(result);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    return new NumberEval(result);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 60 with NumberEval

use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.

the class TestFixed method testOptionalParams.

@Test
public void testOptionalParams() {
    Fixed fixed = new Fixed();
    ValueEval evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789));
    assertTrue(evaluate instanceof StringEval);
    assertEquals("1,234.57", ((StringEval) evaluate).getStringValue());
    evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789), new NumberEval(1));
    assertTrue(evaluate instanceof StringEval);
    assertEquals("1,234.6", ((StringEval) evaluate).getStringValue());
    evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789), new NumberEval(1), BoolEval.TRUE);
    assertTrue(evaluate instanceof StringEval);
    assertEquals("1234.6", ((StringEval) evaluate).getStringValue());
    evaluate = fixed.evaluate(new ValueEval[] {}, 1, 1);
    assertTrue(evaluate instanceof ErrorEval);
    evaluate = fixed.evaluate(new ValueEval[] { new NumberEval(1), new NumberEval(1), new NumberEval(1), new NumberEval(1) }, 1, 1);
    assertTrue(evaluate instanceof ErrorEval);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval) Test(org.junit.Test)

Aggregations

NumberEval (org.apache.poi.ss.formula.eval.NumberEval)123 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)101 Test (org.junit.Test)34 StringEval (org.apache.poi.ss.formula.eval.StringEval)29 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)21 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)19 Calendar (java.util.Calendar)17 Date (java.util.Date)13 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)12 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)8 I_MatchPredicate (org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate)5 RefEval (org.apache.poi.ss.formula.eval.RefEval)4 AssertionFailedError (junit.framework.AssertionFailedError)3 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 DateFormatSymbols (java.text.DateFormatSymbols)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 SimpleDateFormat (java.text.SimpleDateFormat)1