use of java.math.RoundingMode in project scout.rt by eclipse.
the class TypeCastUtilityTest method createCustomBigDecimal.
protected BigDecimal createCustomBigDecimal(long l) {
int scale = 10;
int precision = 30;
RoundingMode roundingMode = RoundingMode.HALF_UP;
MathContext mathContext = new MathContext(precision, roundingMode);
BigDecimal customZero = new BigDecimal(l, mathContext).setScale(scale, roundingMode);
return customZero;
}
use of java.math.RoundingMode in project jsr354-ri 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 by JavaMoney.
the class MonetaryRoundingsTest method testGetRoundingIntRoundingMode.
/**
* Test method for
* {@link javax.money.Monetary#getRounding(javax.money.RoundingQuery)} for arithmetic rounding.
* .
*/
@Test
public void testGetRoundingIntRoundingMode() {
MonetaryAmount[] samples = new MonetaryAmount[] { Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("0.0000000001")).create(), Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("1.00000000000023")).create(), Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("1.1123442323")).create(), Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("1.50000000000")).create(), Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("-1.000000003")).create(), Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("-1.100232876532876389")).create(), Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("-1.500000000000")).create() };
int[] scales = new int[] { 0, 1, 2, 3, 4, 5 };
for (MonetaryAmount sample : samples) {
for (int scale : scales) {
for (RoundingMode roundingMode : RoundingMode.values()) {
if (roundingMode == RoundingMode.UNNECESSARY) {
continue;
}
MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(scale).set(roundingMode).build());
BigDecimal dec = sample.getNumber().numberValue(BigDecimal.class);
BigDecimal expected = dec.setScale(scale, roundingMode);
MonetaryAmount r = sample.with(rounding);
assertEquals(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(expected).create(), r);
}
}
}
}
use of java.math.RoundingMode in project jsr354-ri by JavaMoney.
the class AbstractCurrencyConversion method roundFactor.
/**
* Optionally rounds the factor to be used. By default this method will only round
* as much as it is needed, so the factor can be handled by the target amount instance based on its
* numeric capabilities. Rounding is applied only if {@code amount.getContext().getMaxScale() > 0} as follows:
* <ul>
* <li>If the amount provides a {@link MathContext} as context property this is used.</li>
* <li>If the amount provides a {@link RoundingMode}, this is used (default is
* {@code RoundingMode.HALF_EVEN}).</li>
* <li>By default the scale used is scale of the conversion factor. If the acmount allows a higher
* scale based on {@code amount.getContext().getMaxScale()}, this higher scale is used.</li>
* </ul>
*
* @param amount the amount, not null.
* @param factor the factor
* @return the new rounding factor, never null.
*/
protected NumberValue roundFactor(MonetaryAmount amount, NumberValue factor) {
if (amount.getContext().getMaxScale() > 0) {
MathContext mathContext = amount.getContext().get(MathContext.class);
if (mathContext == null) {
int scale = factor.getScale();
if (factor.getScale() > amount.getContext().getMaxScale()) {
scale = amount.getContext().getMaxScale();
}
RoundingMode roundingMode = amount.getContext().get(RoundingMode.class);
if (roundingMode == null) {
roundingMode = RoundingMode.HALF_EVEN;
}
mathContext = new MathContext(scale, roundingMode);
}
return factor.round(mathContext);
}
return factor;
}
use of java.math.RoundingMode in project jsr354-ri by JavaMoney.
the class DefaultMonetaryContextFactory method createMonetaryContextNonNullConfig.
private MonetaryContext createMonetaryContextNonNullConfig(Map<String, String> config, int prec) {
String value = config.get("org.javamoney.moneta.Money.defaults.roundingMode");
RoundingMode rm = Objects.nonNull(value) ? RoundingMode.valueOf(value.toUpperCase(Locale.ENGLISH)) : RoundingMode.HALF_UP;
MonetaryContext mc = MonetaryContextBuilder.of(Money.class).setPrecision(prec).set(rm).set(Money.class).build();
Logger.getLogger(DefaultMonetaryContextFactory.class.getName()).info("Using custom MathContext: precision=" + prec + ", roundingMode=" + rm);
return mc;
}
Aggregations