First, I wonder if the calculation is correct, after all, you are not using the variable parcela
in it. In addition pv
means present value, and no number of plots, so it makes no sense to iterate the number of plots until the pv
. It makes sense to iterate to the prazo
if this corresponds to the number of plots. Within the calculation of each plot, raising a variable to the exponent given by the present value also makes no sense, but the number of the plot yes. This is why your calculation does not work and results in Infinity.
Then you use the methods of BigDecimal
to do the math. In particular, the methods to be used are the add(BigDecimal)
, the subtract(BigDecimal)
, the multiply(BigDecimal)
, the divide(BigDecimal)
and the pow(int)
.
You can also use the builder who receives a int
to convert a int
in a BigDecimal
. When the value to be converted is 0, 1 or 10, you can use the static fields BigDecimal.ZERO
, BigDecimal.ONE
and BigDecimal.TEN
.
Here’s what the code looks like:
private static final BigDecimal CEM = new BigDecimal(100);
// ...
BigDecimal bigPv = new BigDecimal(txtValor.getText());
BigDecimal taxa = new BigDecimal(txtTaxa.getText());
int prazo = Integer.parseInt(txtPrazo.getText());
BigDecimal porcento = taxa.divide(CEM);
BigDecimal j1 = porcento.add(BigDecimal.ONE);
for (int parcela = 1; parcela <= prazo; parcela++) {
BigDecimal j2 = j1.pow(parcela);
BigDecimal j3 = BigDecimal.ONE.subtract(j2);
BigDecimal j4 = taxa.divide(j3);
BigDecimal j5 = j4.multiply(pv);
lP1.setText(String.valueOf(parcela));
lV1.setText(j5.toPlainString());
In the end, you don’t need to convert the BigDecimal
for Double
to then put in the lV1
, just use the method toPlainString()
. It is important not to confuse it with the toString()
and the toEngineeringString()
that have a slightly different functioning.
Notice that I kept the creation of CEM
and the j1
outside the for
for the sake of performance, so that it is not necessary to always recreate the same BigDecimal
s. The j1
does not depend on the value of parcela
, only of the da taxa
, soon can stay out of the for
. In the case of CEM
, as she is a constant, I put as static.
I also note that the lP1.setText
and the lV1.setText
within the for
should not be what you want, because only the last computed value will remain.
Another however is that you decided to use BigDecimal
because I was getting Infinity as a result. It turns out that the real problem is that its calculation form was wrong (as I explained in the first paragraph). Use BigDecimal
would not solve the problem of using the wrong formula. Thus, you could re-use double
, although I do not recommend, because BigDecimal
is recommended for financial calculations.
Finally, I recommend you review the above calculation, because although I noticed a mistake in your way of performing the calculation, it does not mean that there are no other errors.
pv
means present value and no number of plots. Within the calculation, you have to use exponentiation with the portion number, or else all the plots would be equal and that’s not the purpose. See more in my answer downstairs.– Victor Stafusa