Refine search
private MathContext createMathContext(Long precision, String roundingMode){ if (precision == null) { precision = Long.valueOf(0); } RoundingMode rm = RoundingMode.HALF_UP; if (!StringUtils.isEmpty(roundingMode) || roundingMode != null) { rm = RoundingMode.valueOf(roundingMode); } return new MathContext(precision.intValue(), rm); }
/** * Returns the hash code for this {@code MathContext} instance. * * @return the hash code for this {@code MathContext}. */ @Override public int hashCode() { // Make place for the necessary bits to represent 8 rounding modes return ((precision << 3) | roundingMode.ordinal()); }
@GwtIncompatible // sqrt public void testSqrtNegativeAlwaysThrows() { for (int x : NEGATIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : RoundingMode.values()) { try { IntMath.sqrt(x, mode); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } } }
public static MathContext instantiate(SerializationStreamReader streamReader) throws SerializationException { return new MathContext(streamReader.readInt(), RoundingMode.values()[streamReader.readInt()]); }
public static void serialize(SerializationStreamWriter streamWriter, MathContext instance) throws SerializationException { streamWriter.writeInt(instance.getPrecision()); streamWriter.writeInt(instance.getRoundingMode().ordinal()); }
/** * Creates the rounded Operator from mathContext * @param mathContext the math context, not null. * @return the {@link MonetaryOperator} using the {@link MathContext} used in parameter * @throws NullPointerException when the {@link MathContext} is null * @throws IllegalArgumentException when the {@link MathContext#getPrecision()} is lesser than zero * @throws IllegalArgumentException when the mathContext is {@link MathContext#getRoundingMode()} is {@link RoundingMode#UNNECESSARY} * @see MathContext */ public static PrecisionContextRoundedOperator of(MathContext mathContext) { Objects.requireNonNull(mathContext); if(RoundingMode.UNNECESSARY.equals(mathContext.getRoundingMode())) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the RoundingMode.UNNECESSARY on MathContext"); } if(mathContext.getPrecision() <= 0) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the zero precision on MathContext"); } return new PrecisionContextRoundedOperator(mathContext); }
/** * Returns a new {@code BigDecimal} instance with the specified scale. * <p> * If the new scale is greater than the old scale, then additional zeros are * added to the unscaled value. In this case no rounding is necessary. * <p> * If the new scale is smaller than the old scale, then trailing digits are * removed. If these trailing digits are not zero, then the remaining * unscaled value has to be rounded. For this rounding operation the * specified rounding mode is used. * * @param newScale * scale of the result returned. * @param roundingMode * rounding mode to be used to round the result. * @return a new {@code BigDecimal} instance with the specified scale. * @throws IllegalArgumentException * if {@code roundingMode} is not a valid rounding mode. * @throws ArithmeticException * if {@code roundingMode == ROUND_UNNECESSARY} and rounding is * necessary according to the given scale. */ public BigDecimal setScale(int newScale, int roundingMode) { return setScale(newScale, RoundingMode.valueOf(roundingMode)); }
@Override public String toString() { return this.getClass().getSimpleName() + " " + myMathContext.getPrecision() + ":" + myScale + " " + myMathContext.getRoundingMode().toString(); }
throw invalidMathContext("Missing precision", s); this.precision = Integer.parseInt(precisionString); } catch (NumberFormatException nfe) { throw invalidMathContext("Bad precision", s); throw invalidMathContext("Missing rounding mode", s); this.roundingMode = RoundingMode.valueOf(s.substring(roundingModeStart)); checkValid();
public CodegenExpression initCtorScoped() { if (mathContext == null) { return constantNull(); } return newInstance(MathContext.class, constant(mathContext.getPrecision()), enumValue(RoundingMode.class, mathContext.getRoundingMode().name())); }
public BigDecimalConverter withRoundingMode(RoundingMode mode) { properties.put(BigDecimalConverter.ROUNDING_MODE, mode.toString()); return this; }
@Override public int getRoundingMode() { RoundingMode jdkMode = fJdkNfmt.getRoundingMode(); int icuMode = BigDecimal.ROUND_UP; if (jdkMode.equals(RoundingMode.CEILING)) { icuMode = BigDecimal.ROUND_CEILING; } else if (jdkMode.equals(RoundingMode.DOWN)) { icuMode = BigDecimal.ROUND_DOWN; } else if (jdkMode.equals(RoundingMode.FLOOR)) { icuMode = BigDecimal.ROUND_FLOOR; } else if (jdkMode.equals(RoundingMode.HALF_DOWN)) { icuMode = BigDecimal.ROUND_HALF_DOWN; } else if (jdkMode.equals(RoundingMode.HALF_EVEN)) { icuMode = BigDecimal.ROUND_HALF_EVEN; } else if (jdkMode.equals(RoundingMode.HALF_UP)) { icuMode = BigDecimal.ROUND_HALF_UP; } else if (jdkMode.equals(RoundingMode.UNNECESSARY)) { icuMode = BigDecimal.ROUND_UNNECESSARY; } else if (jdkMode.equals(RoundingMode.UP)) { icuMode = BigDecimal.ROUND_UP; } return icuMode; }
public static DecimalFormat scale2Format(final int scale, final RoundingMode roundingMode) { final String cacheKey = scale + roundingMode.name(); DecimalFormat result = DECIMAL_FORMAT_MAP.get(cacheKey); if (result == null) { result = new DecimalFormat(scale2FormatStr(scale)); result.setRoundingMode(roundingMode); DECIMAL_FORMAT_MAP.put(cacheKey, result); } return result; }
public static MathContext instantiate(SerializationStreamReader streamReader) throws SerializationException { return new MathContext(streamReader.readInt(), RoundingMode.values()[streamReader.readInt()]); }
/** * {@icu} Sets the MathContext used by this format. * * @param newValue desired MathContext * @see #getMathContext * @stable ICU 4.2 */ public void setMathContext(java.math.MathContext newValue) { mathContext = new MathContext(newValue.getPrecision(), MathContext.SCIENTIFIC, false, (newValue.getRoundingMode()).ordinal()); }
/** * Creates the rounded Operator from scale and roundingMode * @param mathContext the math context, not null. * @return the {@link MonetaryOperator} using the scale and {@link RoundingMode} used in parameter * @throws NullPointerException when the {@link MathContext} is null * @throws IllegalArgumentException if {@link MathContext#getPrecision()} is lesser than zero * @throws IllegalArgumentException if {@link MathContext#getRoundingMode()} is {@link RoundingMode#UNNECESSARY} * @see RoundingMode */ public static PrecisionScaleRoundedOperator of(int scale, MathContext mathContext) { Objects.requireNonNull(mathContext); if(RoundingMode.UNNECESSARY.equals(mathContext.getRoundingMode())) { throw new IllegalArgumentException("To create the ScaleRoundedOperator you cannot use the RoundingMode.UNNECESSARY on MathContext"); } if(mathContext.getPrecision() <= 0) { throw new IllegalArgumentException("To create the ScaleRoundedOperator you cannot use the zero precision on MathContext"); } return new PrecisionScaleRoundedOperator(scale, mathContext); }
/** * Returns a new {@code BigDecimal} whose value is {@code this / divisor}. * The scale of the result is the scale of {@code this}. If rounding is * required to meet the specified scale, then the specified rounding mode * {@code roundingMode} is applied. * * @param divisor * value by which {@code this} is divided. * @param roundingMode * rounding mode to be used to round the result. * @return {@code this / divisor} rounded according to the given rounding * mode. * @throws NullPointerException * if {@code divisor == null}. * @throws IllegalArgumentException * if {@code roundingMode} is not a valid rounding mode. * @throws ArithmeticException * if {@code divisor == 0}. * @throws ArithmeticException * if {@code roundingMode == ROUND_UNNECESSARY} and rounding is * necessary according to the scale of this. */ public BigDecimal divide(BigDecimal divisor, int roundingMode) { return divide(divisor, scale, RoundingMode.valueOf(roundingMode)); }
@Override public String toString() { return this.getClass().getSimpleName() + " " + myMathContext.getPrecision() + ":" + myScale + " " + myMathContext.getRoundingMode().toString(); }
throw invalidMathContext("Missing precision", s); this.precision = Integer.parseInt(precisionString); } catch (NumberFormatException nfe) { throw invalidMathContext("Bad precision", s); throw invalidMathContext("Missing rounding mode", s); this.roundingMode = RoundingMode.valueOf(s.substring(roundingModeStart)); checkValid();
"roundingMode=" + roundingMode.toString();