Well, I made this class to solve your problem:
public final class PrecoComJuros {
private final BigDecimal valorBase;
private final BigDecimal valorParcelado; // pv
private final BigDecimal entrada;
private final int numeroParcelas;
private final BigDecimal taxaJuros;
private final BigDecimal valorParcela;
private final BigDecimal valorTotal; // pmt
private final BigDecimal valorJuros;
public PrecoComJuros(BigDecimal valorBase, BigDecimal entrada, int numeroParcelas, BigDecimal taxaJuros) {
if (numeroParcelas <= 0) throw new IllegalArgumentException();
if (taxaJuros.compareTo(BigDecimal.ZERO) < 0) throw new IllegalArgumentException();
this.valorBase = valorBase;
this.entrada = entrada;
this.numeroParcelas = numeroParcelas;
this.taxaJuros = taxaJuros;
BigDecimal juros = taxaJuros.divide(CEM); // i
this.valorParcelado = valorBase.subtract(entrada);
if (taxaJuros.compareTo(BigDecimal.ZERO) == 0) {
this.valorParcela = valorParcelado.divide(BigDecimal.valueOf(numeroParcelas), 2, RoundingMode.HALF_EVEN);
} else {
BigDecimal potencia = juros.add(BigDecimal.ONE).pow(numeroParcelas);
BigDecimal denominador = BigDecimal.ONE.subtract(BigDecimal.ONE.divide(potencia, 20, RoundingMode.HALF_EVEN));
this.valorParcela = valorParcelado.multiply(juros).divide(denominador, 2, RoundingMode.HALF_EVEN);
}
this.valorJuros = valorParcela.multiply(BigDecimal.valueOf(numeroParcelas));
this.valorTotal = entrada.add(valorJuros);
}
public BigDecimal getValorBase() {
return valorBase;
}
public BigDecimal getValorParcelado() {
return valorParcelado;
}
public BigDecimal getEntrada() {
return entrada;
}
public int getNumeroParcelas() {
return numeroParcelas;
}
public BigDecimal getTaxaJuros() {
return taxaJuros;
}
public BigDecimal getValorParcela() {
return valorParcela;
}
public BigDecimal getValorTotal() {
return valorTotal;
}
public BigDecimal getValorJuros() {
return valorJuros;
}
}
Here is her test:
public static void main(String[] args) {
System.out.println(Arrays.toString(" : : ".split(":")));
PrecoComJuros p = new PrecoComJuros(BigDecimal.valueOf(30_000), BigDecimal.valueOf(10_000), 24, BigDecimal.valueOf(5));
System.out.println("Valor da parcela: " + p.getValorParcela());
System.out.println("Juros total: " + p.getValorJuros());
System.out.println("Valor total: " + p.getValorTotal());
}
Some comments on implementation:
Pass a negative exponent to the method pow
makes an exception. The solution to this is to note that , and therefore we can eliminate the need to use a negative exponent.
There is a special treatment for the case of zero interest rate. If there was no such treatment, a division by zero would occur.
Values with number of installments equal to zero or negative or with negative interest rates are rejected.
The plot value is calculated with cents (2 digits after the comma). Intermediate calculations, however use up to 20 houses after the comma.
And what is your doubt?
– Maniero
@mustache doubt eh I believe I’m doing wrong because I can’t get the result. So I asked how to do ?
– FernandoPaiva
Fernando, you have the mathematical calculus?
– viana
This is the formula : PMT = PV. i / 1 - (1 + i) -n ?
– viana
What is PMT, PV, i, and n? Try to put in your question a caption!
– viana
@seamusd for what I researched there is a formula for this, that eh I left enunciated in the method
calcular()
, is what I’m trying to do. I tried to follow this video: https://www.youtube.com/watch?v=P6V2QOv1qLE– FernandoPaiva
Fernando, edit the question and inform that you need to do this using Bigdecimal, because using primitives, you may have some problems accurately. Thus, you make evident the need of Bigdecimal.
– user28595
@diegofm I think a good tip, even if it is installment, involves money, it is implied that can not be double, and his code already showed that it was
BigDecimal
from the beginning. Staff need to read the question before answering.– Maniero