Problem in the format_money helper
It seems there is a problem in the format_money helper. If we specify a divider (as in a TextoColumn::make(...)->money("EUR", 100) ), it will perform a bcdiv without any precision specified. So, if we have a monetary value in cents, it will cut the cents part. Or am I missing anything?
4 Replies
Thanks for the pointer. But if I use the cast in the model, isn't it automatically converted to float on database read (and to int on write), making all my operations with that column be using floats, getting back to the original rounding and precision issues? I believe that this is a presentation problem, not a model problem. I'd still like to have the model always in cents, just present it in euros...
Let's see if Dan or someone else from the team can answer, as i see it you're working against the core principles tho
I probably am, but I don't understand how, or why. It seems obvious to me that all operations regarding money should be done using integers (in cents) precisely to avoid floating point errors. Using casts at the model layer has the effect of working directly with floats during calculations. If I have something like an invoice line with "quantity" and "price_without_taxes", and price_without_taxes is casted to float, when calculating the invoice total I'll introduce errors:
$total = 0;
foreach($invoice->lines as $line) {
$lineTotal = $line->price_without_taxes * $line->quantity; // 1st possible precision errors
$total += $lineTotal; // sum of rounding errors
And then, storing the $total to the invoice model, the cast would enter again, converting the float back to integer and store it in the database