Search in sources :

Example 16 with NumberValue

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

the class DefaultNumberValueTest method shouldReturnAmountFractionNumerator.

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

Example 17 with NumberValue

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

the class DefaultNumberValueTest method shouldReturnNumberType.

@Test
public void shouldReturnNumberType() {
    NumberValue numberValueLong = DefaultNumberValue.of(10L);
    NumberValue numberValueInteger = DefaultNumberValue.of(10);
    NumberValue numberValueDouble = DefaultNumberValue.of(10D);
    NumberValue numberValueBigDecimal = DefaultNumberValue.of(BigDecimal.TEN);
    NumberValue numberValueBigInteger = DefaultNumberValue.of(BigInteger.TEN);
    assertEquals(numberValueLong.getNumberType(), Long.class);
    assertEquals(numberValueInteger.getNumberType(), Integer.class);
    assertEquals(numberValueDouble.getNumberType(), Double.class);
    assertEquals(numberValueBigDecimal.getNumberType(), BigDecimal.class);
    assertEquals(numberValueBigInteger.getNumberType(), BigInteger.class);
}
Also used : NumberValue(javax.money.NumberValue) Test(org.testng.annotations.Test)

Example 18 with NumberValue

use of javax.money.NumberValue in project jsr354-ri-bp 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 (rate == null || !amount.getCurrency().equals(rate.getBaseCurrency())) {
        throw new CurrencyConversionException(amount.getCurrency(), this.termCurrency, this.conversionContext);
    }
    NumberValue factor = rate.getFactor();
    factor = roundFactor(amount, factor);
    Integer scale = rate.getContext().get(KEY_SCALE, Integer.class);
    if (scale == null || 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 19 with NumberValue

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

the class DefaultNumberValueTest method shouldReturnNumberValue.

@Test
public void shouldReturnNumberValue() {
    NumberValue numberValue = DefaultNumberValue.of(BigDecimal.valueOf(21));
    assertEquals(Long.valueOf(21L), numberValue.numberValue(Long.class));
    assertEquals(Integer.valueOf(21), numberValue.numberValue(Integer.class));
    assertEquals(Float.valueOf(21), numberValue.numberValue(Float.class));
    assertEquals(Byte.valueOf((byte) 21), numberValue.numberValue(Byte.class));
    assertEquals(Short.valueOf((short) 21), numberValue.numberValue(Short.class));
    assertEquals(new AtomicLong(21L).get(), numberValue.numberValue(AtomicLong.class).get());
    assertEquals(new AtomicInteger(21).get(), numberValue.numberValue(AtomicInteger.class).get());
    assertEquals(BigDecimal.valueOf(21L), numberValue.numberValue(BigDecimal.class));
    assertEquals(BigInteger.valueOf(21L), numberValue.numberValue(BigInteger.class));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) NumberValue(javax.money.NumberValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 20 with NumberValue

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

the class DefaultNumberValueTest method shouldReturnLongValueExact.

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

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