public static BigDecimal divide(final BigDecimal bd1, final BigDecimal bd2) throws EvaluationException { if (bd2.signum() == 0) { // prevent a division by zero .. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARITHMETIC_VALUE); } final BigDecimal divide = bd1.divide(bd2, LibFormulaBoot.GLOBAL_SCALE, BigDecimal.ROUND_HALF_UP); return NumberUtil.removeTrailingZeros(divide); } }
public static BigDecimal divide( final BigDecimal bd1, final BigDecimal bd2 ) throws EvaluationException { if ( bd2.signum() == 0 ) { // prevent a division by zero .. throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARITHMETIC_VALUE ); } final BigDecimal divide = bd1.divide( bd2, LibFormulaBoot.GLOBAL_SCALE, BigDecimal.ROUND_HALF_UP ); return NumberUtil.removeTrailingZeros( divide ); } }
public static BigDecimal performTuneRounding( BigDecimal n ) { try { // no need to go further if the value is already an integer n.setScale( ROUND_SCALE ); return n; } catch ( ArithmeticException e ) { //ignore and continue } final BigDecimal round; if ( n.signum() < 0 ) { n = n.subtract( INT_TEST_DELTA ); round = n.setScale( ROUND_SCALE, BigDecimal.ROUND_UP ); } else { n = n.add( INT_TEST_DELTA ); round = n.setScale( ROUND_SCALE, BigDecimal.ROUND_DOWN ); } if ( n.compareTo( round ) == 0 ) { return n; } return NumberUtil.removeTrailingZeros( round ); }
public static BigDecimal performTuneRounding(BigDecimal n) { try { // no need to go further if the value is already an integer n.setScale(ROUND_SCALE); return n; } catch(ArithmeticException e) { //ignore and continue } final BigDecimal round; if(n.signum()<0) { n = n.subtract(INT_TEST_DELTA); round = n.setScale(ROUND_SCALE, BigDecimal.ROUND_UP); } else { n = n.add(INT_TEST_DELTA); round = n.setScale(ROUND_SCALE, BigDecimal.ROUND_DOWN); } if (n.compareTo(round) == 0) { return n; } return NumberUtil.removeTrailingZeros(round); }
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters ) throws EvaluationException { final int parameterCount = parameters.getParameterCount(); if ( parameterCount != 1 ) { throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE ); } final Type type1 = parameters.getType( 0 ); final Object value1 = parameters.getValue( 0 ); final Number result = context.getTypeRegistry().convertToNumber( type1, value1 ); if ( result == null ) { throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE ); } final double d = StrictMath.log( result.doubleValue() ); final BigDecimal divide = new BigDecimal( d ).divide( LOG10BASE, LibFormulaBoot.GLOBAL_SCALE, BigDecimal.ROUND_HALF_UP ); return new TypeValuePair( NumberType.GENERIC_NUMBER, NumberUtil.removeTrailingZeros( divide ) ); } }
public TypeValuePair evaluate(final FormulaContext context, final ParameterCallback parameters) throws EvaluationException { final int parameterCount = parameters.getParameterCount(); if (parameterCount != 1) { throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE); } final Type type1 = parameters.getType(0); final Object value1 = parameters.getValue(0); final Number result = context.getTypeRegistry().convertToNumber(type1, value1); if (result == null) { throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE); } final double d = StrictMath.log(result.doubleValue()); final BigDecimal divide = new BigDecimal(d).divide(LOG10BASE, LibFormulaBoot.GLOBAL_SCALE, BigDecimal.ROUND_HALF_UP); return new TypeValuePair(NumberType.GENERIC_NUMBER, NumberUtil.removeTrailingZeros(divide)); } }