Search in sources :

Example 26 with NumberValue

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

the class DefaultNumberValueTest method shouldReturnDoubleExact.

@Test
public void shouldReturnDoubleExact() {
    NumberValue numberValue = DefaultNumberValue.of(BigDecimal.valueOf(132.21));
    assertEquals(numberValue.doubleValueExact(), 132.21);
}
Also used : NumberValue(javax.money.NumberValue) Test(org.testng.annotations.Test)

Example 27 with NumberValue

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

the class DefaultNumberValueTest method shouldReturnIntValueExact.

@Test
public void shouldReturnIntValueExact() {
    NumberValue numberValue = DefaultNumberValue.of(BigDecimal.valueOf(132));
    assertEquals(numberValue.intValueExact(), 132);
}
Also used : NumberValue(javax.money.NumberValue) Test(org.testng.annotations.Test)

Example 28 with NumberValue

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

the class ReciprocalOperator method apply.

/**
 * Gets the amount as reciprocal / multiplicative inversed value (1/n).
 * <p>
 * E.g. 'EUR 2.0' will be converted to 'EUR 0.5'.
 *
 * @return the reciprocal / multiplicative inversed of the amount
 * @throws ArithmeticException if the arithmetic operation failed
 */
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
    Objects.requireNonNull(amount, "Amount required.");
    NumberValue num = amount.getNumber();
    BigDecimal one = new BigDecimal("1.0").setScale(num.getScale() < 5 ? 5 : num.getScale(), BigDecimal.ROUND_HALF_EVEN);
    return amount.getFactory().setNumber(one.divide(num.numberValue(BigDecimal.class), RoundingMode.HALF_EVEN)).create();
}
Also used : NumberValue(javax.money.NumberValue) BigDecimal(java.math.BigDecimal)

Example 29 with NumberValue

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

the class AbstractCurrencyConversion method apply.

/**
 * Method that converts the source {@link MonetaryAmount} to an
 * {@link MonetaryAmount} based on the {@link ExchangeRate} of this
 * conversion.
 * @param amount The source amount
 * @return The converted amount, never null.
 * @throws CurrencyConversionException if conversion failed, or the required data is not available.
 * @see #getExchangeRate(MonetaryAmount)
 */
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
    if (termCurrency.equals(Objects.requireNonNull(amount).getCurrency())) {
        return amount;
    }
    ExchangeRate rate = getExchangeRate(amount);
    if (Objects.isNull(rate) || !amount.getCurrency().equals(rate.getBaseCurrency())) {
        throw new CurrencyConversionException(amount.getCurrency(), this.termCurrency, null);
    }
    NumberValue factor = rate.getFactor();
    factor = roundFactor(amount, factor);
    Integer scale = rate.getContext().get(KEY_SCALE, Integer.class);
    if (Objects.isNull(scale) || scale < 0) {
        return amount.multiply(factor).getFactory().setCurrency(rate.getCurrency()).create();
    } else {
        return amount.multiply(factor).getFactory().setCurrency(rate.getCurrency()).create().with(MonetaryOperators.rounding(scale));
    }
}
Also used : ExchangeRate(javax.money.convert.ExchangeRate) NumberValue(javax.money.NumberValue) CurrencyConversionException(javax.money.convert.CurrencyConversionException)

Example 30 with NumberValue

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

the class ReciprocalOperator method apply.

/**
 * Gets the amount as reciprocal / multiplicative inversed value (1/n).
 * <p>
 * E.g. 'EUR 2.0' will be converted to 'EUR 0.5'.
 *
 * @return the reciprocal / multiplicative inversed of the amount
 * @throws ArithmeticException if the arithmetic operation failed
 */
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
    Objects.requireNonNull(amount, "Amount required.");
    NumberValue num = amount.getNumber();
    BigDecimal one = new BigDecimal("1.0").setScale(num.getScale() < 5 ? 5 : num.getScale(), BigDecimal.ROUND_HALF_EVEN);
    return amount.getFactory().setNumber(one.divide(num.numberValue(BigDecimal.class), RoundingMode.HALF_EVEN)).create();
}
Also used : NumberValue(javax.money.NumberValue) BigDecimal(java.math.BigDecimal)

Aggregations

NumberValue (javax.money.NumberValue)40 Test (org.testng.annotations.Test)36 BigDecimal (java.math.BigDecimal)6 BigInteger (java.math.BigInteger)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 CurrencyConversionException (javax.money.convert.CurrencyConversionException)2 ExchangeRate (javax.money.convert.ExchangeRate)2