Java Code Examples for javax.money.MonetaryAmount#multiply()

The following examples show how to use javax.money.MonetaryAmount#multiply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BalloonLoanPayment.java    From javamoney-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amountPV       the present value, not null.
 * @param balloonAmount  the balloon amount, not null and currency compatible with {@code amountPV}.
 * @param rateAndPeriods the target rate and periods, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amountPV, MonetaryAmount balloonAmount,
                                       RateAndPeriods rateAndPeriods) {
    Objects.requireNonNull(rateAndPeriods);
    Rate rate = rateAndPeriods.getRate();
    int periods = rateAndPeriods.getPeriods();

    BigDecimal factor2 = rate.get().divide(
            one().subtract(
                    one().add(rate.get()).pow(-periods, MathContext.DECIMAL64)), MathContext.DECIMAL64);
    MonetaryAmount factor1 = amountPV.subtract(
            balloonAmount.getFactory().setNumber(
                    balloonAmount.getNumber().numberValue(BigDecimal.class).divide(
                    one().add(rate.get()).pow(periods, MathContext.DECIMAL64), MathContext.DECIMAL64)).create());
    return factor1.multiply(factor2);
}
 
Example 2
Source File: JavaMoneyUnitManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenArithmetic_whenStringified_thanEqualsAmount() {
    CurrencyUnit usd = Monetary.getCurrency("USD");

    Money moneyof = Money.of(12, usd);
    MonetaryAmount fstAmtUSD = Monetary
      .getDefaultAmountFactory()
      .setCurrency(usd)
      .setNumber(200.50)
      .create();
    MonetaryAmount oneDolar = Monetary
      .getDefaultAmountFactory()
      .setCurrency("USD")
      .setNumber(1)
      .create();
    Money subtractedAmount = Money
      .of(1, "USD")
      .subtract(fstAmtUSD);
    MonetaryAmount multiplyAmount = oneDolar.multiply(0.25);
    MonetaryAmount divideAmount = oneDolar.divide(0.25);

    assertEquals("USD", usd.toString());
    assertEquals("USD 1", oneDolar.toString());
    assertEquals("USD 200.5", fstAmtUSD.toString());
    assertEquals("USD 12", moneyof.toString());
    assertEquals("USD -199.5", subtractedAmount.toString());
    assertEquals("USD 0.25", multiplyAmount.toString());
    assertEquals("USD 4", divideAmount.toString());
}
 
Example 3
Source File: AnnualPercentageYield.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount  the base amount, not null.
 * @param rate    the target rate, not null.
 * @param periods the periods, >= 0.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, Rate rate, int periods) {
    if(periods==0){
        return amount.getFactory().setNumber(0.0).create();
    }
    final BigDecimal ONE = new BigDecimal(1, MathContext.DECIMAL64);
    BigDecimal baseFactor = rate.get().divide(BigDecimal.valueOf(periods),MathContext.DECIMAL64)
            .add(ONE);
    return amount.multiply(baseFactor.pow(periods).subtract(ONE));
}
 
Example 4
Source File: FutureValueOfAnnuityWithContCompounding.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount         the first payment
 * @param rateAndPeriods The rate and periods, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, RateAndPeriods rateAndPeriods) {
    Objects.requireNonNull(amount, "Amount required");
    Rate rate = Objects.requireNonNull(rateAndPeriods.getRate(), "Rate required");
    int periods = rateAndPeriods.getPeriods();
    // FVofA/CC = CF * [ (e.pow(r*n) - 1) / ((e.pow(r) - 1)) ]
    double num = Math.pow(Math.E, rate.get().doubleValue() * periods) - 1.0;
    double denum = Math.pow(Math.E, rate.get().doubleValue()) - 1.0;
    BigDecimal factor = new BigDecimal(num, CalculationContext.mathContext())
            .divide(new BigDecimal(denum, CalculationContext.mathContext()), CalculationContext.mathContext().getRoundingMode());
    return amount.multiply(factor);
}
 
Example 5
Source File: FutureValueOfAnnuity.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount         the first payment
 * @param rateAndPeriods The rate and periods, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, RateAndPeriods rateAndPeriods) {
    // Am * (((1 + r).pow(n))-1/rate)
    Rate rate = rateAndPeriods.getRate();
    int periods = rateAndPeriods.getPeriods();
    return amount.multiply(one().add(rate.get()).pow(periods).subtract(one()).divide(
            rate.get(),CalculationContext.mathContext()));
}
 
Example 6
Source File: PresentValueGrowingAnnuityPayment.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount                 the dividend payment
 * @param discountRateAndPeriods The discount rate and periods, not null.
 * @param growthRate             The growth rate, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, RateAndPeriods discountRateAndPeriods, Rate growthRate) {
    Objects.requireNonNull(amount, "amount required");
    Objects.requireNonNull(discountRateAndPeriods, "discountRateAndPeriods required");
    Objects.requireNonNull(growthRate, "growthRate required");
    Rate discountRate = discountRateAndPeriods.getRate();
    int periods = discountRateAndPeriods.getPeriods();
    BigDecimal numerator = discountRate.get().subtract(growthRate.get());
    BigDecimal denum = BigDecimal.ONE.subtract(BigDecimal.ONE
            .add(growthRate.get())
            .divide(BigDecimal.ONE.add(discountRate.get()), CalculationContext.mathContext())
            .pow(periods, CalculationContext.mathContext()));
    return amount.multiply(numerator.divide(denum, CalculationContext.mathContext()));
}
 
Example 7
Source File: PresentValueContinuousCompounding.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount         the first payment
 * @param rateAndPeriods The rate and periods, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, RateAndPeriods rateAndPeriods) {
    Objects.requireNonNull(amount, "Amount required");
    Objects.requireNonNull(rateAndPeriods, "Rate required");
    Rate rate = rateAndPeriods.getRate();
    int periods = rateAndPeriods.getPeriods();
    BigDecimal fact = CalculationContext.one().divide(
            new BigDecimal(Math.pow(Math.E, rate
            .get().doubleValue() * periods), CalculationContext.mathContext()),
            CalculationContext.mathContext());
    return amount.multiply(fact);
}
 
Example 8
Source File: FutureValueGrowingAnnuity.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param firstPayment the first payment
 * @param discountRate The rate perperiod, not null. If the rate is less than 0, the result will be zero.
 * @param growthRate   The growth rate, not null.
 * @param periods      the target periods, >= 0.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount firstPayment, Rate discountRate, Rate growthRate,
                                       int periods) {
    if(discountRate.equals(growthRate)){
        throw new MonetaryException("Discount rate and growth rate cannot be the same.");
    }
    if(discountRate.get().signum()<0){
        return firstPayment.getFactory().setNumber(0.0d).create();
    }
    BigDecimal num = CalculationContext.one().add(discountRate.get()).pow(periods)
            .subtract(CalculationContext.one().add(growthRate.get()).pow(periods));
    BigDecimal denum = discountRate.get().subtract(growthRate.get());
    return firstPayment.multiply(num.divide(denum, CalculationContext.mathContext()));
}
 
Example 9
Source File: FutureValueOfAnnuityDue.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount         the first payment
 * @param rateAndPeriods The rate and periods, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, RateAndPeriods rateAndPeriods) {
    // Am * (((1 + r).pow(n))-1/rate)
    Rate rate = rateAndPeriods.getRate();
    int periods = rateAndPeriods.getPeriods();
    BigDecimal base = CalculationContext.one().add(rate.get());
    BigDecimal counter = base.pow(periods, CalculationContext.mathContext()).subtract(BigDecimal.ONE);
    return amount.multiply(
            counter.divide(rate.get(), CalculationContext.mathContext()).multiply(base));
}
 
Example 10
Source File: FutureValueWithContinuousCompounding.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount         the first payment
 * @param rateAndPeriods The rate and periods, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, RateAndPeriods rateAndPeriods) {
    Objects.requireNonNull(amount, "Amount required");
    Rate rate = Objects.requireNonNull(rateAndPeriods.getRate(), "Rate required");
    int periods = rateAndPeriods.getPeriods();
    MonetaryAmount pv = PresentValue.calculate(amount, rateAndPeriods);
    BigDecimal fact = new BigDecimal(String.valueOf(Math.pow(Math.E, rate.get().doubleValue() * periods)));
    return pv.multiply(fact);
}
 
Example 11
Source File: SimpleInterest.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount         the first payment
 * @param rateAndPeriods The rate and periods, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, RateAndPeriods rateAndPeriods) {
    Objects.requireNonNull(amount, "Amount required");
    Objects.requireNonNull(rateAndPeriods, "Rate required");
    int periods = rateAndPeriods.getPeriods();
    if(periods==0 || amount.signum()==0){
        return amount.getFactory().setNumber(0.0).create();
    }
    Rate rate = rateAndPeriods.getRate();
    BigDecimal factor = rate.get().multiply(
            BigDecimal.valueOf(periods), CalculationContext.mathContext());
    return amount.multiply(factor);
}
 
Example 12
Source File: Rate.java    From javamoney-lib with Apache License 2.0 4 votes vote down vote up
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
	return amount.multiply(rate);
}
 
Example 13
Source File: PermilOperator.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the permil of the amount.
 * <p>
 * This returns the monetary amount in permil. For example, for 10% 'EUR
 * 2.35' will return 0.235.
 * <p>
 * This is returned as a {@code MonetaryAmount}.
 *
 * @return the permil result of the amount, never {@code null}
 */
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
	Objects.requireNonNull(amount, "Amount required.");
	return amount.multiply(permilValue);
}
 
Example 14
Source File: PercentOperator.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the percentage of the amount.
 * <p>
 * This returns the monetary amount in percent. For example, for 10% 'EUR
 * 2.35' will return 0.235.
 * <p>
 * This is returned as a {@code MonetaryAmount}.
 *
 * @return the percent result of the amount, never {@code null}
 */
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
	Objects.requireNonNull(amount, "Amount required.");
	return amount.multiply(percentValue);
}
 
Example 15
Source File: BasisPoint.java    From javamoney-lib with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the permil of the amount.
 * <p>
 * This returns the monetary amount in permil. For example, for 10% 'EUR
 * 2.35' will return 0.235.
 * <p>
 * This is returned as a {@code MonetaryAmount}.
 *
 * @param amount      the amount
 * @param basisPoints the basis points
 * @return the permil result of the amount, never {@code null}
 */
public static MonetaryAmount calculate(MonetaryAmount amount, Number basisPoints) {
	return amount.multiply(calcBasisPoint(basisPoints));
}
 
Example 16
Source File: BasisPoint.java    From javamoney-lib with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the permil of the amount.
 * <p>
 * This returns the monetary amount in permil. For example, for 10% 'EUR
 * 2.35' will return 0.235.
 * <p>
 * This is returned as a {@code MonetaryAmount}.
 *
 * @param amount amount of the permil applied being created to.
 * @return the permil of the given amount, never {@code null}
 */
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
	return amount.multiply(basisPointValue);
}
 
Example 17
Source File: FutureValue.java    From javamoney-lib with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount         the base amount, not null.
 * @param rateAndPeriods the target rate and periods, not null.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, RateAndPeriods rateAndPeriods) {
    BigDecimal f = (CalculationContext.one().add(rateAndPeriods.getRate().get())).pow(rateAndPeriods.getPeriods());
    return amount.multiply(f);
}
 
Example 18
Source File: EstimatedEarnings.java    From javamoney-lib with Apache License 2.0 2 votes vote down vote up
/**
 * Calculates the estimated earnings.
 *
 * @param projectedSales           the projected sales
 * @param projectedNetProfitMargin the projected net profit margin
 * @return the estimated earnings
 */
public static MonetaryAmount calculate(MonetaryAmount projectedSales, BigDecimal projectedNetProfitMargin) {
    return projectedSales.multiply(projectedNetProfitMargin);
}