use of java.math.RoundingMode in project jsr354-ri by JavaMoney.
the class DefaultRoundingProvider method getRounding.
/**
* Evaluate the rounding that match the given query.
*
* @return the (shared) default rounding instances matching, never null.
*/
public MonetaryRounding getRounding(RoundingQuery roundingQuery) {
if (roundingQuery.get(LocalDateTime.class) != null || roundingQuery.get(LocalDate.class) != null) {
return null;
}
CurrencyUnit currency = roundingQuery.getCurrency();
if (currency != null) {
RoundingMode roundingMode = roundingQuery.get(RoundingMode.class);
if (roundingMode == null) {
roundingMode = RoundingMode.HALF_EVEN;
}
// Simple hack to honor: https://en.wikipedia.org/wiki/Cash_rounding#Rounding_with_1.00_intervals
if (Boolean.TRUE.equals(roundingQuery.getBoolean("cashRounding"))) {
if ("CHF".equals(currency.getCurrencyCode())) {
return new DefaultCashRounding(currency, RoundingMode.HALF_UP, 5);
} else if ("SEK".equals(currency.getCurrencyCode())) {
return new DefaultCashRounding(currency, RoundingMode.HALF_UP, 100);
} else if ("NOK".equals(currency.getCurrencyCode())) {
return new DefaultCashRounding(currency, RoundingMode.HALF_UP, 100);
} else if ("¨CNY".equals(currency.getCurrencyCode())) {
return new DefaultCashRounding(currency, RoundingMode.HALF_UP, 10);
} else if ("¨HKD".equals(currency.getCurrencyCode())) {
return new DefaultCashRounding(currency, RoundingMode.HALF_UP, 10);
} else if ("¨NZD".equals(currency.getCurrencyCode())) {
return new DefaultCashRounding(currency, RoundingMode.HALF_DOWN, 10);
} else {
return new DefaultCashRounding(currency, 1);
}
}
return new DefaultRounding(currency, roundingMode);
}
Integer scale = roundingQuery.getScale();
if (scale == null) {
scale = 2;
}
MathContext mc = roundingQuery.get(MathContext.class);
RoundingMode roundingMode = roundingQuery.get(RoundingMode.class);
if (mc != null) {
return new DefaultRounding(scale, mc.getRoundingMode());
} else if (roundingMode != null) {
return new DefaultRounding(scale, roundingMode);
} else if (roundingQuery.getRoundingName() != null && DEFAULT_ROUNDING_NAME.equals(roundingQuery.getRoundingName())) {
return Monetary.getDefaultRounding();
}
return null;
}
use of java.math.RoundingMode in project jsr354-ri by JavaMoney.
the class DefaultCashRounding method apply.
/*
* (non-Javadoc)
*
* @see javax.money.MonetaryFunction#apply(java.lang.Object)
*/
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
Objects.requireNonNull(amount, "Amount required.");
// 1 extract BD value, round according the default fraction units
int scale = this.context.getInt(SCALE_KEY);
RoundingMode roundingMode = this.context.get(RoundingMode.class);
BigDecimal num = amount.getNumber().numberValue(BigDecimal.class).setScale(scale, roundingMode);
// 2 evaluate minor units and remainder
long minors = num.movePointRight(num.scale()).longValueExact();
int minimalMinors = this.context.getInt(MINMINORS_KEY);
long factor = minors / minimalMinors;
long low = minimalMinors * factor;
long high = minimalMinors * (factor + 1);
if (minors - low > high - minors) {
minors = high;
} else if (minors - low < high - minors) {
minors = low;
} else {
switch(roundingMode) {
case HALF_UP:
case UP:
case HALF_EVEN:
minors = high;
break;
default:
minors = low;
}
}
return amount.getFactory().setCurrency(amount.getCurrency()).setNumber(BigDecimal.valueOf(minors).movePointLeft(scale)).create();
}
use of java.math.RoundingMode in project jsr354-ri-bp by JavaMoney.
the class MonetaryRoundedFactoryBuilderTest method shouldReturnScaleMathContextOperator.
@Test
public void shouldReturnScaleMathContextOperator() {
int precision = 6;
int scale = 3;
RoundingMode roundingMode = RoundingMode.HALF_EVEN;
MonetaryRoundedFactory factory = MonetaryRoundedFactory.withRoundingMode(roundingMode).withScale(scale).withPrecision(precision).build();
assertNotNull(factory);
MonetaryOperator roundingOperator = factory.getRoundingOperator();
assertNotNull(roundingOperator);
assertTrue(PrecisionScaleRoundedOperator.class.isInstance(roundingOperator));
PrecisionScaleRoundedOperator result = PrecisionScaleRoundedOperator.class.cast(roundingOperator);
assertEquals(precision, result.getMathContext().getPrecision());
assertEquals(scale, result.getScale());
assertEquals(roundingMode, result.getMathContext().getRoundingMode());
}
use of java.math.RoundingMode in project jsr354-ri-bp by JavaMoney.
the class MonetaryRoundedFactoryBuilderTest method shouldReturnMathContextScaleOperator.
@Test
public void shouldReturnMathContextScaleOperator() {
int precision = 6;
int scale = 3;
RoundingMode roundingMode = RoundingMode.HALF_EVEN;
MonetaryRoundedFactory factory = MonetaryRoundedFactory.withRoundingMode(roundingMode).withPrecision(precision).withScale(scale).build();
assertNotNull(factory);
MonetaryOperator roundingOperator = factory.getRoundingOperator();
assertNotNull(roundingOperator);
assertTrue(PrecisionScaleRoundedOperator.class.isInstance(roundingOperator));
PrecisionScaleRoundedOperator result = PrecisionScaleRoundedOperator.class.cast(roundingOperator);
assertEquals(precision, result.getMathContext().getPrecision());
assertEquals(scale, result.getScale());
assertEquals(roundingMode, result.getMathContext().getRoundingMode());
}
use of java.math.RoundingMode in project jsr354-ri-bp by JavaMoney.
the class MonetaryRoundedFactoryBuilderTest method shouldReturnScaleRoundingOperator.
@Test
public void shouldReturnScaleRoundingOperator() {
int scale = 6;
RoundingMode roundingMode = RoundingMode.HALF_EVEN;
MonetaryRoundedFactory factory = MonetaryRoundedFactory.withRoundingMode(roundingMode).withScale(scale).build();
assertNotNull(factory);
MonetaryOperator roundingOperator = factory.getRoundingOperator();
assertNotNull(roundingOperator);
assertTrue(ScaleRoundedOperator.class.isInstance(roundingOperator));
ScaleRoundedOperator result = ScaleRoundedOperator.class.cast(roundingOperator);
assertEquals(scale, result.getScale());
assertEquals(roundingMode, result.getRoundingMode());
}
Aggregations