Generate values and dates of parcels in a datatable C #

0

Hello,

I have a form where a Total Sale Value is placed, the Number of Payment Parcels. Then I would like my DataTable that is located on the other Form to display the values and due dates with their value divided by the number of months.

For example:

       Valor Total: 200

       Prazo: 3

No datatable has to appear:

 parcela    |  valor  | Data Vencimento
    01      |  66,67  |   02/02/2018
    02      |  66,67  |   02/03/2018
    03      |  66,67  |   02/04/2018

Did you understand my doubt? Could someone help me?

    
asked by anonymous 02.02.2018 / 20:31

1 answer

3

Here is an example demonstration.

decimal valorTotal = 200.00M;
int numeroParcelas = 3;
DateTime dataPrimeiroVencimento = DateTime.Now;

decimal valorParcela = Math.Round(valorTotal / numeroParcelas, 2);
decimal valorDiferenca = valorTotal - valorParcela * numeroParcelas;    

for (int i = 0; i < numeroParcelas; i++)
{

    //Calculo dos valores;
    string parcela = (i + 1).ToString().PadLeft(2, '0');
    string valor = !(i + 1 == numeroParcelas) ? valorParcela.ToString() : (valorParcela + valorDiferenca).ToString();
    string dataVencimento = dataPrimeiroVencimento.AddMonths(i).ToShortDateString();


    //Exemplo do resultado
    Console.WriteLine(parcela + " | " + valor + " | " + dataVencimento);
}

Output:

01 | 66,67 | 02/02/2018
02 | 66,67 | 02/03/2018
03 | 66,66 | 02/04/2018
    
02.02.2018 / 21:19