Search in sources :

Example 36 with RoundingMode

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;
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 37 with RoundingMode

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());
}
Also used : RoundingMode(java.math.RoundingMode) MonetaryOperator(javax.money.MonetaryOperator) Test(org.testng.annotations.Test)

Example 38 with RoundingMode

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);
            }
        }
    }
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 39 with RoundingMode

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;
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext)

Example 40 with RoundingMode

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;
}
Also used : RoundingMode(java.math.RoundingMode) MonetaryContext(javax.money.MonetaryContext)

Aggregations

RoundingMode (java.math.RoundingMode)254 BigDecimal (java.math.BigDecimal)139 BigInteger (java.math.BigInteger)88 MathContext (java.math.MathContext)86 GwtIncompatible (com.google.common.annotations.GwtIncompatible)39 Test (org.testng.annotations.Test)13 NumberFormat (java.text.NumberFormat)10 MonetaryOperator (javax.money.MonetaryOperator)8 DecimalFormat (java.text.DecimalFormat)6 IDataMap (permafrost.tundra.data.IDataMap)5 Test (org.junit.Test)3 MifosConfigurationManager (org.mifos.config.business.MifosConfigurationManager)3 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Insets (java.awt.Insets)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 MonetaryContext (javax.money.MonetaryContext)2 ButtonGroup (javax.swing.ButtonGroup)2 JLabel (javax.swing.JLabel)2