Search in sources :

Example 41 with RoundingMode

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

Example 42 with RoundingMode

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

Example 43 with RoundingMode

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

Example 44 with RoundingMode

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

Example 45 with RoundingMode

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

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