@Override public MathContext getMathContext() { return delegate.getMathContext(); }
@Override public MathContext getMathContext() { MathContext mc = FEATURES.get().mathContext; return mc == null? super.getMathContext() : mc; } }
/** * Ensure a big decimal is rounded by this arithmetic scale and rounding mode. * @param number the big decimal to round * @return the rounded big decimal * @since 2.1 */ public BigDecimal roundBigDecimal(final BigDecimal number) { int mscale = getMathScale(); if (mscale >= 0) { return number.setScale(mscale, getMathContext().getRoundingMode()); } else { return number; } }
return BigDecimal.ZERO; return roundBigDecimal(new BigDecimal(string, getMathContext())); } else if (val instanceof Double) { if (!Double.isNaN(((Double) val).doubleValue())) { return roundBigDecimal(new BigDecimal(val.toString(), getMathContext())); } else { return BigDecimal.ZERO; return roundBigDecimal(new BigDecimal(val.toString(), getMathContext())); } else if (val instanceof Character) { int i = ((Character) val).charValue();
throw new ArithmeticException("/"); BigDecimal result = l.divide(r, getMathContext()); return narrowBigDecimal(left, right, result);
throw new ArithmeticException("%"); BigDecimal remainder = l.remainder(r, getMathContext()); return narrowBigDecimal(left, right, remainder);
/** * Subtract the right value from the left. * @param left first value * @param right second value * @return left - right. */ public Object subtract(Object left, Object right) { if (left == null && right == null) { return controlNullNullOperands(); } // if either are floating point (double or float) use double if (isFloatingPointNumber(left) || isFloatingPointNumber(right)) { double l = toDouble(left); double r = toDouble(right); return new Double(l - r); } // if either are bigdecimal use that type if (left instanceof BigDecimal || right instanceof BigDecimal) { BigDecimal l = toBigDecimal(left); BigDecimal r = toBigDecimal(right); BigDecimal result = l.subtract(r, getMathContext()); return narrowBigDecimal(left, right, result); } // otherwise treat as integers BigInteger l = toBigInteger(left); BigInteger r = toBigInteger(right); BigInteger result = l.subtract(r); return narrowBigInteger(left, right, result); }
/** * Multiply the left value by the right. * @param left first value * @param right second value * @return left * right. */ public Object multiply(Object left, Object right) { if (left == null && right == null) { return controlNullNullOperands(); } // if either are floating point (double or float) use double if (isFloatingPointNumber(left) || isFloatingPointNumber(right)) { double l = toDouble(left); double r = toDouble(right); return new Double(l * r); } // if either are bigdecimal use that type if (left instanceof BigDecimal || right instanceof BigDecimal) { BigDecimal l = toBigDecimal(left); BigDecimal r = toBigDecimal(right); BigDecimal result = l.multiply(r, getMathContext()); return narrowBigDecimal(left, right, result); } // otherwise treat as integers BigInteger l = toBigInteger(left); BigInteger r = toBigInteger(right); BigInteger result = l.multiply(r); return narrowBigInteger(left, right, result); }
BigDecimal l = toBigDecimal(left); BigDecimal r = toBigDecimal(right); BigDecimal result = l.add(r, getMathContext()); return narrowBigDecimal(left, right, result);