This is a very short sample of what I have in my database.
CREATE TEMPORARY TABLE temp_ledger(
book_entry INT,
credit DOUBLE(24, 8),
debit DOUBLE(24, 8)
);
INSERT INTO
temp_ledger(book_entry, credit, debit)
VALUES
(1, 17.54500000, 0.00000000),
(1, 0.00000000, 14.50000000),
(1, 0.00000000, 3.04500000),
(2, 0.00000000, 99.85500000),
(2, 95.10000000, 0.00000000),
(2, 4.75500000, 0.00000000);
SELECT
book_entry,
SUM(ROUND(debit, 2)) as sum_of_rounded_debit,
SUM(ROUND(credit, 2)) as sum_of_rounded_credit,
ROUND(SUM(debit), 2) as round_of_summed_debit,
ROUND(SUM(credit), 2) as round_of_summed_credit,
SUM(debit) as summed_debit,
SUM(credit) as summed_credit
FROM
temp_ledger
GROUP BY
book_entry;
DROP TEMPORARY TABLE temp_ledger;
Output
temp_ledger
book_entry | sum_of_rounded_debit | sum_of_rounded_credit | round_of_summed_debit | round_of_summed_credit | summed_debit | summed_credit |
---|---|---|---|---|---|---|
1 | 17,54 | 17,55 | 17,55 | 17,55 | 17,54500000 | 17,54500000 |
2 | 99,86 | 99,86 | 99,86 | 99,85 | 99,85500000 | 99,85500000 |
I don't understand why sum_of_rounded_debit
for book entry 1 is 17.54:
- 14.50 rounded should be 14.50
- 3.045 rounded should be 3.05
And round_of_summed_credit
for book entry 2 is 99.85 if summed_credit is 99.855, why does rounding that give me 99.85 and not 99.86
I understand that sometimes decimals can be finicky but round_of_summed_credit
in particular has me wondering "What?".
This came up trying to find errors in Dolibarr's llx_accounting_bookkeeping table, since sum of round give a certain rows as mismatched between credit and debit while round of sum give a different set of rows.
Using MariaDB 10.5.8