How to optimize plot calculation on "broken" values

Asked

Viewed 1,041 times

0

I have an application that, depending on the format of the portion that the user selects, the system will return the amount of that portion. For example: Assuming an order of R $ 50.000,00 in two times, the system will store in the bank two records containing R $ 25.000,00.

The problem is time to calculate a "broken" value. For example: The user generates an order of R $ 100.000,00 and installment he has three times, the system will keep three results of R$ 33,333.33.

The problem is time to get the resulting value, because if I know that the value of the portion was 3 and multiply the value R $ 33.333,33 get R$ 99.999,99, that is, that 1 centavo ends up missing what there in front generates a discrepant value when obtaining certain reports.

I’ve come to realize that tax money systems do this to perfection...

How can I generate plots (mostly broken), containing an extra penny when needed?

  • I do not know what was your difficulty, I made a test here stating the value 100.000,00 without the point and the comma, and in multiplication obtained the exact value. You could edit your question and put the code ?

2 answers

5


It has more than one strategy, but almost all are based on the accumulation of error and discount at the end.

In a very simple version of the implementation, you calculate the value of the installment and subtract from parcela * (n - 1) to know the latest.

Example:

 valor = 10.000
 num_parcelas = 3
 valor_parcela = 3333,33

 ultima_parcela = valor - valor_parcela * (num_parcelas - 1)

That is to say:

 ultima_parcela = 10.000 - 3333,33 * (3 - 1)

Resulting in:

 3333,34

There are many other ways to do it, some less complex, others more.

If you want to "increment" the algorithm, you can take the cents difference of the installments and go "distributing" in the others so that you don’t accumulate everything in the last one (in case you give more than a penny difference).

Another idea is to make one if that, in case the value is higher, put as first installment, and if it is lower, in the end, so the payer always has the feeling of the value "fall" at the end. You have to see what’s best in your specific case.

Anyway, for most strategies, the above formula is a good starting point.

  • Just one detail, which I think is worth living in comment: if you’re going to make the division round down from n plots, the maximum rest is n-1 pennies. You can get that rest like this ((valor*100)%parcelas)/100 (or find another more direct formula)

  • 1

    @Jeffersonquesado I think fits in a preparation of the same answer. If this is the case, with more time, I do it in PHP code, and I will take your comment into consideration (you can leave it there so I don’t forget :) )

  • Thank you for the reply. I will implement the solution.

0

Register in the database 3 decimal places, then use the function number_format to round to 2 decimal places when recovering the database values before summing.

Run this example and see:

$parcela = 33333.333;
echo number_format($parcela * 3, 2, ',', '');

Remember that a division by default in PHP returns more than 2 decimal places, just set it to number_format before writing to the database:

$total = 100000;
$totalParaGuardar = number_format($total / 3, 3, '.', '');
  • Could improve your answer editing her and put an example?

  • Excuse me @Samuelfontebasso I ended up editing along with you

  • Now it’s OK :)

  • All right, so your answer doesn’t get in line Low quality publications.

  • I didn’t know about this line, I’ll look it up. Thank you!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.