Search in sources :

Example 26 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class RoudingMonetaryAmountOperator method apply.

@Override
public MonetaryAmount apply(MonetaryAmount amount) {
    Objects.requireNonNull(amount, "Amount required.");
    CurrencyUnit currency = amount.getCurrency();
    int scale = scaleOptional.orElse(currency.getDefaultFractionDigits());
    BigDecimal value = amount.getNumber().numberValue(BigDecimal.class).setScale(scale, roundingMode);
    return amount.getFactory().setNumber(value).create();
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) BigDecimal(java.math.BigDecimal)

Example 27 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class JDKCurrencyProvider method getCurrencies.

/**
 * Return a {@link CurrencyUnit} instances matching the given
 * {@link javax.money.CurrencyContext}.
 *
 * @param currencyQuery the {@link javax.money.CurrencyContext} containing the parameters determining the query. not null.
 * @return the corresponding {@link CurrencyUnit}, or null, if no such unit
 * is provided by this provider.
 */
public Set<CurrencyUnit> getCurrencies(CurrencyQuery currencyQuery) {
    Set<CurrencyUnit> result = new HashSet<>();
    if (!currencyQuery.getCurrencyCodes().isEmpty()) {
        for (String code : currencyQuery.getCurrencyCodes()) {
            CurrencyUnit cu = CACHED.get(code);
            if (cu != null) {
                result.add(cu);
            }
        }
        return result;
    }
    if (!currencyQuery.getCountries().isEmpty()) {
        for (Locale country : currencyQuery.getCountries()) {
            CurrencyUnit cu = getCurrencyUnit(country);
            if (cu != null) {
                result.add(cu);
            }
        }
        return result;
    }
    if (!currencyQuery.getNumericCodes().isEmpty()) {
        for (Integer numCode : currencyQuery.getNumericCodes()) {
            List<CurrencyUnit> cus = getCurrencyUnits(numCode);
            result.addAll(cus);
        }
        return result;
    }
    // No constraints defined, return all.
    result.addAll(CACHED.values());
    return result;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Example 28 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri by JavaMoney.

the class GroupMonetarySummaryStatistics method combine.

public GroupMonetarySummaryStatistics combine(GroupMonetarySummaryStatistics another) {
    Objects.requireNonNull(another);
    for (CurrencyUnit keyCurrency : another.groupSummary.keySet()) {
        groupSummary.putIfAbsent(keyCurrency, new DefaultMonetarySummaryStatistics(keyCurrency));
        groupSummary.merge(keyCurrency, another.groupSummary.get(keyCurrency), MonetarySummaryStatistics::combine);
    }
    return this;
}
Also used : CurrencyUnit(javax.money.CurrencyUnit)

Example 29 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri-bp by JavaMoney.

the class MonetaryOperatorsTest method shouldRouding.

@Test
public void shouldRouding() {
    CurrencyUnit euro = Monetary.getCurrency("EUR");
    MonetaryAmount money = Money.parse("EUR 2.355432");
    MonetaryAmount result = MonetaryOperators.rounding().apply(money);
    assertNotNull(result);
    assertEquals(result.getCurrency(), euro);
    assertEquals(2.36, result.getNumber().doubleValue());
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

Example 30 with CurrencyUnit

use of javax.money.CurrencyUnit in project jsr354-ri-bp by JavaMoney.

the class PercentOperatorTest method shouldReturnNegativeValue.

@Test
public void shouldReturnNegativeValue() {
    CurrencyUnit currency = Monetary.getCurrency("BHD");
    MonetaryAmount money = Money.parse("BHD -200.0");
    MonetaryAmount result = operator.apply(money);
    assertEquals(result.getCurrency(), currency);
    assertEquals(result.getNumber().doubleValue(), -20.0);
}
Also used : CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) Test(org.testng.annotations.Test)

Aggregations

CurrencyUnit (javax.money.CurrencyUnit)116 Test (org.testng.annotations.Test)78 MonetaryAmount (javax.money.MonetaryAmount)57 BigDecimal (java.math.BigDecimal)14 Currency (java.util.Currency)8 MonetaryException (javax.money.MonetaryException)6 ExchangeRate (javax.money.convert.ExchangeRate)6 RoundedMoney (org.javamoney.moneta.RoundedMoney)6 IOException (java.io.IOException)5 MathContext (java.math.MathContext)5 MonetaryParseException (javax.money.format.MonetaryParseException)5 Money (org.javamoney.moneta.Money)4 ExchangeRateBuilder (org.javamoney.moneta.convert.ExchangeRateBuilder)3 DefaultNumberValue (org.javamoney.moneta.spi.DefaultNumberValue)3 Test (org.junit.Test)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2