How can I split the parcels by adding just exact values for each parcel, and will one parcel be left with an extra value?

2

Example: The division of 100 (entry value) by 3 (quantity of parcels) is equal to 33.33, where 3 * 33.33 = 99.99. Being that the right is to stay a parcel with the value 33.34.

Java code

public void parcelar() {
    listaParcela = new ArrayList<>();
    //if (compra.getQtdparcelas() == null) {
    //   if (compra.getQtdparcelas() > 1) {
    Double totalP = compra.getValortotalentrada() / 
    compra.getQtdparcelas().doubleValue();
    DateTime data = new DateTime(pagar.getVencimento());
    for (int i = 1; i <= compra.getQtdparcelas(); i++) {
        Cpagar par = new Cpagar();
        par.setVencimento(data.toDate());
        par.setValor(totalP);
        par.setFormaPagamentoId(pagar.getFormaPagamentoId());
        par.setObs(campoconsulta);
        par.setContas(compra);
        compra.getContasList().add(par);
        listaParcela.add(par);
        data = data.plusMonths(1);
    }
}
    
asked by anonymous 22.10.2017 / 04:37

1 answer

3

I did as follows:

double diferenca = compra.getValortotalentrada() - (totalP * compra.getQtdparcelas().doubleValue());

Let's assume that the value is 100 so the totalLP would be 33.33 as in your example.

The logic is as follows:

diferenca = 100  - (33.33 * 3)
diferenca = 100  - (99.99)
direfenta = 0.01

When entering the loop it adds this difference in the first plot:

par.setValor(totalP + diferenca);

And in the end not to add in the other attribute 0 in difference:

diferenca = 0;

Follow the example:

public void parcelar() {
    listaParcela = new ArrayList<>();
    //if (compra.getQtdparcelas() == null) {
    //   if (compra.getQtdparcelas() > 1) {
    Double totalP = compra.getValortotalentrada() / 
    compra.getQtdparcelas().doubleValue();
    DateTime data = new DateTime(pagar.getVencimento());
    double diferenca = compra.getValortotalentrada() - (totalP * compra.getQtdparcelas().doubleValue());
    for (int i = 1; i <= compra.getQtdparcelas(); i++) {
        Cpagar par = new Cpagar();
        par.setVencimento(data.toDate());
        par.setValor(totalP + diferenca);
        par.setFormaPagamentoId(pagar.getFormaPagamentoId());
        par.setObs(campoconsulta);
        par.setContas(compra);
        compra.getContasList().add(par);
        listaParcela.add(par);
        data = data.plusMonths(1);
        diferenca = 0;
    }
}
    
22.10.2017 / 04:54