Bug 36049 - Rounding prices sometimes leads to incorrect results
Summary: Rounding prices sometimes leads to incorrect results
Status: Pushed to stable
Alias: None
Product: Koha
Classification: Unclassified
Component: Acquisitions (show other bugs)
Version: Main
Hardware: All All
: P3 normal
Assignee: Raphael Straub
QA Contact: Victor Grousset/tuxayo
URL:
Keywords:
Depends on:
Blocks: 37937
  Show dependency treegraph
 
Reported: 2024-02-08 13:44 UTC by Raphael Straub
Modified: 2024-11-18 22:19 UTC (History)
10 users (show)

See Also:
Change sponsored?: Sponsored
Patch complexity: Medium patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
24.11.00,24.05.06
Circulation function:


Attachments
Bug 36049: Fix price rounding and add a unit test (2.32 KB, patch)
2024-02-08 14:32 UTC, Raphael Straub
Details | Diff | Splinter Review
Bug 36049: Fix price rounding and add a unit test (2.37 KB, patch)
2024-02-08 18:56 UTC, David Nind
Details | Diff | Splinter Review
Bug 36049: Add unit test (1.00 KB, patch)
2024-03-03 02:05 UTC, Victor Grousset/tuxayo
Details | Diff | Splinter Review
Bug 36049: Fix price rounding (1.70 KB, patch)
2024-03-03 02:05 UTC, Victor Grousset/tuxayo
Details | Diff | Splinter Review
Bug 36049: Fix price rounding (1.73 KB, patch)
2024-03-03 02:07 UTC, Victor Grousset/tuxayo
Details | Diff | Splinter Review
Bug 36049: Add unit test (1.06 KB, patch)
2024-09-17 01:03 UTC, Victor Grousset/tuxayo
Details | Diff | Splinter Review
Bug 36049: Fix price rounding (1.79 KB, patch)
2024-09-17 01:03 UTC, Victor Grousset/tuxayo
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Raphael Straub 2024-02-08 13:44:41 UTC
Example:
Create an order line in acquisitions with vendor price 18.90 and a discount of 5 % results in a total (tax excl.) of 17.95. The correct value in the database (ecost_tax_excluded) is 17.955000 which should be rounded to 17.96.

The problem is that all financial calculations in Perl and JavaScript are done with binary floating-point arithmetic instead of decimal arithmetic. For a short introduction to this topic see https://floating-point-gui.de.

The solution is to use Math::BigFloat in Perl and bignumber.js (https://github.com/MikeMcl/bignumber.js) or big.js (https://github.com/MikeMcl/big.js) in JavaScript for every financial calculation.
Comment 1 Raphael Straub 2024-02-08 14:32:28 UTC
Created attachment 161918 [details] [review]
Bug 36049: Fix price rounding and add a unit test

This patch only fixes price rounding by using Math::BigFloat.

To test:
1) Create an order line in acquisitions with vendor price 18.90 and a
   discount of 5 %.
2) Verify that the total (tax excl.) in the order table on the basket
   page is 17.95.
3) Apply the patch.
4) Reload the basket page.
5) Verify that the total (tax excl.) in the order table on the basket
   page now is 17.96, which is correct.
6) Run: prove t/Number/Price.t

Sponsored-by: Karlsruhe Institute of Technology (KIT)
Comment 2 David Nind 2024-02-08 18:02:21 UTC
Hi Raphael.

I had a go at testing, however the price doesn't change for me - it stays as 17.95.

David
Comment 3 David Nind 2024-02-08 18:43:30 UTC
(In reply to David Nind from comment #2)
> Hi Raphael.
> 
> I had a go at testing, however the price doesn't change for me - it stays as
> 17.95.
> 
> David

Replying to myself - it worked when I set OrderPriceRounding to round.

Also wondering whether this is the same/similar to bug 35114
Comment 4 David Nind 2024-02-08 18:56:28 UTC
Created attachment 161951 [details] [review]
Bug 36049: Fix price rounding and add a unit test

This patch only fixes price rounding by using Math::BigFloat.

To test:
1) Create an order line in acquisitions with vendor price 18.90 and a
   discount of 5 %.
2) Verify that the total (tax excl.) in the order table on the basket
   page is 17.95.
3) Apply the patch.
4) Reload the basket page.
5) Verify that the total (tax excl.) in the order table on the basket
   page now is 17.96, which is correct.
6) Run: prove t/Number/Price.t

Sponsored-by: Karlsruhe Institute of Technology (KIT)
Signed-off-by: David Nind <david@davidnind.com>
Comment 5 Michaela Sieber 2024-02-09 08:37:37 UTC
(In reply to David Nind from comment #3)
> (In reply to David Nind from comment #2)
> > Hi Raphael.
> > 
> > I had a go at testing, however the price doesn't change for me - it stays as
> > 17.95.
> > 
> > David
> 
> Replying to myself - it worked when I set OrderPriceRounding to round.
> 
> Also wondering whether this is the same/similar to bug 35114


Hey David, 

sorry, we forgot to mention that syspref OrderPriceRounding should be enabled to "Round" to nearest cent.
Comment 6 Raphael Straub 2024-02-09 09:03:46 UTC
(In reply to David Nind from comment #3)
> Also wondering whether this is the same/similar to bug 35114
The similarity to bug 35114 is that both bugs deal with rounding issues. Thats why I added this bug to "See Also".

I think that the patch in bug 35114 only solves part of the problem by rearranging rounding operations and using a nearest rounding mode instead of truncation in JavaScript. Yes, it improves the situation as there are fewer cases with incorrect results, but cases with incorrect results still remain as the underlying problem is not using decimal arithmetic. For example "Math.round(18.90 * (100 - 5)) / 100" in JavaScript evaluates to "17.95" instead of "17.96". This can only be fixed by using a decimal arithmetic package like bignumber.js or big.js.
Comment 7 David Nind 2024-02-10 03:11:20 UTC
Thanks Raphael! That helps. I'll have another go at testing bug 35114.
Comment 8 Victor Grousset/tuxayo 2024-03-03 02:05:56 UTC
Created attachment 162699 [details] [review]
Bug 36049: Add unit test

Sponsored-by: Karlsruhe Institute of Technology (KIT)
Signed-off-by: David Nind <david@davidnind.com>
Comment 9 Victor Grousset/tuxayo 2024-03-03 02:05:59 UTC Comment hidden (obsolete)
Comment 10 Victor Grousset/tuxayo 2024-03-03 02:07:35 UTC
Created attachment 162701 [details] [review]
Bug 36049: Fix price rounding

This patch only fixes price rounding by using Math::BigFloat.

To test:
0) Enable syspref OrderPriceRounding
1) Create an order line in acquisitions with vendor price 18.90 and a
   discount of 5 %.
2) Verify that the total (tax excl.) in the order table on the basket
   page is 17.95.
3) Apply the patch.
4) Reload the basket page.
5) Verify that the total (tax excl.) in the order table on the basket
   page now is 17.96, which is correct.
6) Run: prove t/Number/Price.t

Sponsored-by: Karlsruhe Institute of Technology (KIT)
Signed-off-by: David Nind <david@davidnind.com>
Comment 11 Victor Grousset/tuxayo 2024-03-03 02:36:53 UTC
In basket view, shouldn't "Budgeted cost tax exc." also honor OrderPriceRounding ?
Same about "Budgeted cost tax inc" and "GST".

In the new order form, "Total" (and "Budgeted cost:" depending on previous question) now aren't consistent with the basket view. It's the previous step and the same data should have the same value.

So we would have basket's "total tax exc" honoring well OrderPriceRounding but there is still buggy rounding around. Either for the same data on the previous page or on the same page close to "total tax exc".

--- 

Commit was split to confirm that the test fails without implementation. That shows the test does it's work :)
Comment 12 Katrin Fischer 2024-09-02 15:48:08 UTC
(In reply to Victor Grousset/tuxayo from comment #11)
> In basket view, shouldn't "Budgeted cost tax exc." also honor
> OrderPriceRounding ?
> Same about "Budgeted cost tax inc" and "GST".

If it doesn't honor it right now, then this seems a bit out of scope here and would be better suited for a separate bug for discussion/decision.

> In the new order form, "Total" (and "Budgeted cost:" depending on previous
> question) now aren't consistent with the basket view. It's the previous step
> and the same data should have the same value.

I believe that this calculation is done with JavaScript only and might require a different kind of fix. It might be also not following the system preference?

> So we would have basket's "total tax exc" honoring well OrderPriceRounding
> but there is still buggy rounding around. Either for the same data on the
> previous page or on the same page close to "total tax exc".

Can you explain this one a bit differently?
Comment 13 Victor Grousset/tuxayo 2024-09-17 01:03:07 UTC
(In reply to Katrin Fischer from comment #12)
> (In reply to Victor Grousset/tuxayo from comment #11)
> > In basket view, shouldn't "Budgeted cost tax exc." also honor
> > OrderPriceRounding ?
> > Same about "Budgeted cost tax inc" and "GST".
> 
> If it doesn't honor it right now, then this seems a bit out of scope here
> and would be better suited for a separate bug for discussion/decision.

Ok. It introduces an inconsistency (17.95 & 17.96 in the same page) but now that it's known and deemed fine for this bug. Not blocking then.


> > In the new order form, "Total" (and "Budgeted cost:" depending on previous
> > question) now aren't consistent with the basket view. It's the previous step
> > and the same data should have the same value.
> 
> I believe that this calculation is done with JavaScript only and might
> require a different kind of fix. It might be also not following the system
> preference?

Ok, same as above, not a issue for here then :)
 
> > So we would have basket's "total tax exc" honoring well OrderPriceRounding
> > but there is still buggy rounding around. Either for the same data on the
> > previous page or on the same page close to "total tax exc".
> 
> Can you explain this one a bit differently?

That was a sum up of the two previous points. But failed since it actually doesn't help and adds confusion ^^"
Comment 14 Victor Grousset/tuxayo 2024-09-17 01:03:38 UTC
Created attachment 171598 [details] [review]
Bug 36049: Add unit test

Sponsored-by: Karlsruhe Institute of Technology (KIT)
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Comment 15 Victor Grousset/tuxayo 2024-09-17 01:03:41 UTC
Created attachment 171599 [details] [review]
Bug 36049: Fix price rounding

This patch only fixes price rounding by using Math::BigFloat.

To test:
0) Enable syspref OrderPriceRounding
1) Create an order line in acquisitions with vendor price 18.90 and a
   discount of 5 %.
2) Verify that the total (tax excl.) in the order table on the basket
   page is 17.95.
3) Apply the patch.
4) Reload the basket page.
5) Verify that the total (tax excl.) in the order table on the basket
   page now is 17.96, which is correct.
6) Run: prove t/Number/Price.t

Sponsored-by: Karlsruhe Institute of Technology (KIT)
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Comment 16 Victor Grousset/tuxayo 2024-09-17 01:05:48 UTC
Works, makes sense, QA script happy, code looks good, passing QA :)
Comment 17 Katrin Fischer 2024-09-17 07:14:11 UTC
I'll have another look before pushing this as you are in principle right about adding inconsistencies. Maybe Raphael can weigh in here as he might have a better idea having looked at the code more closely?
Comment 18 Raphael Straub 2024-09-18 07:27:06 UTC
(In reply to Katrin Fischer from comment #17)
> I'll have another look before pushing this as you are in principle right
> about adding inconsistencies. Maybe Raphael can weigh in here as he might
> have a better idea having looked at the code more closely?

To fix the whole problem, we would have to change every financial calculation in Koha (in Perl and JavaScript, the database does it right) to use a decimal datatype. I think it is not feasible to do it all at once. So, we should identify isolated parts of Koha that could be fixed independently without introducing new inconsistencies and fix those parts separately.

The patch in this bug fixes a rounding function that is used in several parts of Koha and would introduce at least one inconsistency in the acquisition module (see bug 37937). I would not push this patch until we think that no new inconsistencies are introduced by it.

I wrote this bug report mainly to point out that there is a problem with financial calculations in Koha and to provide an example of how this problem can be solved.
Comment 19 Martin Renvoize (ashimema) 2024-09-30 14:33:57 UTC
Thanks for all the hard work!

Pushed to main for the next 24.11.00 release as RM Assistant
Comment 20 Lucas Gass (lukeg) 2024-11-18 22:19:41 UTC
Backported to 24.05.x for upcoming 24.05.06