Calculate Values of Different Parcels [closed]

0

Well, I have a spreadsheet in Excel with several lines with Product values.

I will import these values into MySQL.

For example:
PRODUCT VALUE
PRODUCT X R $ 700.00

I need to divide these R $ 700.00 into X parcels, however these parcels can not have equal values ... Can anyone help me?

    
asked by anonymous 22.06.2017 / 21:41

1 answer

1

Example - Ideone

$parcelas=11;

$segredo = (1 + $parcelas) * ($parcelas / 2);

$valor="R$ 1900,30";

    //replace para retirar R$ e trim para retirar espaço
    $preco=trim(str_replace("R$","",$valor));

    //armazenando virgula centavos
    $decimal= substr($preco,(strlen($preco)-3),strlen($preco));

    //retirando virgula centavos 
    $preco=(str_replace($decimal,"",$preco));

    //calculando valores das parcelas
    for ($x = 1; $x <= ($parcelas-1); $x++) {
       $parcela = intval((($preco*$x)/$segredo));
       echo ("R$ ". number_format($parcela, 2, ',', '.'))."<br>";
       $somatorio=$somatorio+$parcela;
    }

    //calculo da ultima parcela
    $ultima=$preco-$somatorio;
    echo ("R$ ".($ultima.$decimal));

Want to know what the $segredo variable is? mouse over the area below.

  

The variable $segredo is the sum of the numbers from 1 to the number of parcels. Generalizing, sum of n first natural numbers.

DOCUMENTATION

23.06.2017 / 01:48