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