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());
}
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);
}
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));
}
}
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));
}
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);
}
Aggregations