public final class PrecisionContextRoundedOperator extends Object implements MonetaryOperator
MathContext to does the rounding operations. The implementation will use the precision, in other words, the total number of digits in a number
The derived class will implements the RoundedMoney with this rounding monetary operator
MathContext mathContext = new MathContext(4, RoundingMode.HALF_EVEN);
MonetaryOperator monetaryOperator = PrecisionContextRoundedOperator.of(mathContext);
CurrencyUnit real = Monetary.getCurrency("BRL");
MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real);
MonetaryAmount result = monetaryOperator.apply(money); // BRL 35.35
Case the parameter in MonetaryOperator.apply(MonetaryAmount) be null, the apply will return a NullPointerExceptionof(MathContext),
RoundedMoney,
MonetaryOperator,
BigDecimal.precision()| Modifier and Type | Method and Description |
|---|---|
MonetaryAmount |
apply(MonetaryAmount amount) |
MathContext |
getMathContext() |
static PrecisionContextRoundedOperator |
of(MathContext mathContext)
Creates the rounded Operator from mathContext
|
String |
toString() |
public static PrecisionContextRoundedOperator of(MathContext mathContext)
mathContext - the math context, not null.MonetaryOperator using the MathContext used in parameterNullPointerException - when the MathContext is nullIllegalArgumentException - when the MathContext.getPrecision() is lesser than zeroIllegalArgumentException - when the mathContext is MathContext.getRoundingMode() is RoundingMode.UNNECESSARYMathContextpublic MonetaryAmount apply(MonetaryAmount amount)
apply in interface MonetaryOperatorpublic MathContext getMathContext()
Copyright © 2012–2020 JavaMoney. All rights reserved.