/** * Constructs a new {@code BigDecimal} instance from the given big integer * {@code val}. The scale of the result is {@code 0}. * * @param mc * rounding mode and precision for the result of this operation. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and the new big decimal cannot be represented * within the given precision without rounding. */ public BigDecimal(BigInteger val, MathContext mc) { this(val); inplaceRound(mc); }
/** * Constructs a new {@code BigDecimal} instance from a given unscaled value * {@code unscaledVal} and a given scale. The value of this instance is * {@code unscaledVal * 10<sup>-scale</sup>). The result is rounded according * to the specified math context. * * @param mc * rounding mode and precision for the result of this operation. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and the new big decimal cannot be represented * within the given precision without rounding. * @throws NullPointerException * if {@code unscaledVal == null}. */ public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) { this(unscaledVal, scale); inplaceRound(mc); }
/** * Constructs a new {@code BigDecimal} instance from the given long {@code * val}. The scale of the result is {@code 0}. The result is rounded * according to the specified math context. * * @param val * long value to be converted to a {@code BigDecimal} instance. * @param mc * rounding mode and precision for the result of this operation. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and the new big decimal cannot be represented * within the given precision without rounding. */ public BigDecimal(long val, MathContext mc) { this(val); inplaceRound(mc); }
/** * Constructs a new {@code BigDecimal} instance from the given int {@code * val}. The scale of the result is {@code 0}. The result is rounded * according to the specified math context. * * @param val * int value to be converted to a {@code BigDecimal} instance. * @param mc * rounding mode and precision for the result of this operation. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code c.roundingMode == * UNNECESSARY} and the new big decimal cannot be represented * within the given precision without rounding. */ public BigDecimal(int val, MathContext mc) { this(val,0); inplaceRound(mc); }
/** * Constructs a new {@code BigDecimal} instance from a string representation * given as a character array. The result is rounded according to the * specified math context. * * @param in * array of characters containing the string representation of * this {@code BigDecimal}. * @param mc * rounding mode and precision for the result of this operation. * @throws NumberFormatException * if {@code in} does not contain a valid string representation * of a big decimal. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and the new big decimal cannot be represented * within the given precision without rounding. */ public BigDecimal(char[] in, MathContext mc) { this(in, 0, in.length); inplaceRound(mc); }
/** * Constructs a new {@code BigDecimal} instance from a string representation * given as a character array. * * @param in * array of characters containing the string representation of * this {@code BigDecimal}. * @param offset * first index to be copied. * @param len * number of characters to be used. * @param mc * rounding mode and precision for the result of this operation. * @throws NumberFormatException * if {@code offset < 0 || len <= 0 || offset+len-1 < 0 || * offset+len-1 >= in.length}, or if {@code in} does not * contain a valid string representation of a big decimal. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and the new big decimal cannot be represented * within the given precision without rounding. */ public BigDecimal(char[] in, int offset, int len, MathContext mc) { this(in, offset, len); inplaceRound(mc); }
/** * Constructs a new {@code BigDecimal} instance from the 64bit double * {@code val}. The constructed big decimal is equivalent to the given * double. For example, {@code new BigDecimal(0.1)} is equal to {@code * 0.1000000000000000055511151231257827021181583404541015625}. This happens * as {@code 0.1} cannot be represented exactly in binary. * <p> * To generate a big decimal instance which is equivalent to {@code 0.1} use * the {@code BigDecimal(String)} constructor. * * @param val * double value to be converted to a {@code BigDecimal} instance. * @param mc * rounding mode and precision for the result of this operation. * @throws NumberFormatException * if {@code val} is infinity or not a number. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and the new big decimal cannot be represented * within the given precision without rounding. */ public BigDecimal(double val, MathContext mc) { this(val); inplaceRound(mc); }
/** * Returns a new {@code BigDecimal} whose value is the {@code -this}. The * result is rounded according to the passed context {@code mc}. * * @param mc * rounding mode and precision for the result of this operation. * @return {@code -this} */ public BigDecimal negate(MathContext mc) { BigDecimal result = negate(); result.inplaceRound(mc); return result; }
/** * Returns a new {@code BigDecimal} whose value is {@code this * * multiplicand}. The result is rounded according to the passed context * {@code mc}. * * @param multiplicand * value to be multiplied with {@code this}. * @param mc * rounding mode and precision for the result of this operation. * @return {@code this * multiplicand}. * @throws NullPointerException * if {@code multiplicand == null} or {@code mc == null}. */ public BigDecimal multiply(BigDecimal multiplicand, MathContext mc) { BigDecimal result = multiply(multiplicand); result.inplaceRound(mc); return result; }
/** * Constructs a new {@code BigDecimal} instance from a string * representation. The result is rounded according to the specified math * context. * * @param mc * rounding mode and precision for the result of this operation. * @throws NumberFormatException * if {@code val} does not contain a valid string representation * of a big decimal. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and the new big decimal cannot be represented * within the given precision without rounding. */ public BigDecimal(String val, MathContext mc) { this(val.toCharArray(), 0, val.length()); inplaceRound(mc); }
/** * Returns a {@code BigDecimal} whose value is the absolute value of * {@code this}. The result is rounded according to the passed context * {@code mc}. */ public BigDecimal abs(MathContext mc) { BigDecimal result = (signum() < 0) ? negate() : new BigDecimal(getUnscaledValue(), scale); result.inplaceRound(mc); return result; }
/** * Returns a new {@code BigDecimal} whose value is {@code this}, rounded * according to the passed context {@code mc}. * <p> * If {@code mc.precision = 0}, then no rounding is performed. * <p> * If {@code mc.precision > 0} and {@code mc.roundingMode == UNNECESSARY}, * then an {@code ArithmeticException} is thrown if the result cannot be * represented exactly within the given precision. * * @param mc * rounding mode and precision for the result of this operation. * @return {@code this} rounded according to the passed context. * @throws ArithmeticException * if {@code mc.precision > 0} and {@code mc.roundingMode == * UNNECESSARY} and this cannot be represented within the given * precision. */ public BigDecimal round(MathContext mc) { BigDecimal thisBD = new BigDecimal(getUnscaledValue(), scale); thisBD.inplaceRound(mc); return thisBD; }
accum.inplaceRound(mc); return accum;
/** * Returns a new {@code BigDecimal} whose value is the {@code -this}. The * result is rounded according to the passed context {@code mc}. * * @param mc * rounding mode and precision for the result of this operation. * @return {@code -this} */ public BigDecimal negate(MathContext mc) { BigDecimal result = negate(); result.inplaceRound(mc); return result; }
/** * Returns a new {@code BigDecimal} whose value is the {@code -this}. The * result is rounded according to the passed context {@code mc}. * * @param mc * rounding mode and precision for the result of this operation. * @return {@code -this} */ public BigDecimal negate(MathContext mc) { BigDecimal result = negate(); result.inplaceRound(mc); return result; }
/** * Returns a {@code BigDecimal} whose value is the absolute value of * {@code this}. The result is rounded according to the passed context * {@code mc}. */ public BigDecimal abs(MathContext mc) { BigDecimal result = (signum() < 0) ? negate() : new BigDecimal(getUnscaledValue(), scale); result.inplaceRound(mc); return result; }
/** * Returns a {@code BigDecimal} whose value is the absolute value of * {@code this}. The result is rounded according to the passed context * {@code mc}. */ public BigDecimal abs(MathContext mc) { BigDecimal result = (signum() < 0) ? negate() : new BigDecimal(getUnscaledValue(), scale); result.inplaceRound(mc); return result; }
/** * Returns a {@code BigDecimal} whose value is the absolute value of * {@code this}. The result is rounded according to the passed context * {@code mc}. */ public BigDecimal abs(MathContext mc) { BigDecimal result = (signum() < 0) ? negate() : new BigDecimal(getUnscaledValue(), scale); result.inplaceRound(mc); return result; }
/** * Returns a {@code BigDecimal} whose value is the absolute value of * {@code this}. The result is rounded according to the passed context * {@code mc}. */ public BigDecimal abs(MathContext mc) { BigDecimal result = (signum() < 0) ? negate() : new BigDecimal(getUnscaledValue(), scale); result.inplaceRound(mc); return result; }
/** * Returns a {@code BigDecimal} whose value is the absolute value of * {@code this}. The result is rounded according to the passed context * {@code mc}. */ public BigDecimal abs(MathContext mc) { BigDecimal result = (signum() < 0) ? negate() : new BigDecimal(getUnscaledValue(), scale); result.inplaceRound(mc); return result; }