[Bug 43956] New: Koha::Number::Price::round returns wrong or inconsistent results
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 Bug ID: 43956 Summary: Koha::Number::Price::round returns wrong or inconsistent results Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: enhancement Priority: P5 - low Component: Architecture, internals, and plumbing Assignee: koha-bugs@lists.koha-community.org Reporter: martin.renvoize@openfifth.co.uk QA Contact: testopia@bugs.koha-community.org Blocks: 23728 Target Milestone: --- Koha::Number::Price::round returns wrong or inconsistent results, and its rounding disagrees with Koha::Number::Price::format. This is part of the currency rounding work collected on bug 23728 and is the foundation for the rest of it. Observed on main (round / format / sprintf %.2f): 0.825 round=0.82 format=0.83 sprintf=0.82 -0.825 round=-0.82 format=-0.83 sprintf=-0.82 1.005 round=1 format=1.01 sprintf=1.00 0.285 round=0.28 format=0.29 sprintf=0.28 0.125 round=0.12 format=0.13 sprintf=0.12 0.005 round=00 format=0.01 sprintf=0.01 0.004 round=00 format=0.00 sprintf=0.00 "abc" round=NaN format=0.00 "1,5" round=NaN format=1.00 Causes: - round uses Math::BigFloat with its default half-even mode, while format uses Number::Format on binary floats, so the two disagree. - round returns the string from bdstr, which can be "00" and "NaN". - Input is not validated. Proposed: - Use one decimal-arithmetic implementation (Math::BigFloat) for round and format. - Return numeric values from round, never "00" or "NaN"; throw on non-numeric input. - Add a rounding mode parameter: half_away_from_zero (default), half_up, half_down, half_even (banker's), half_odd. - Keep decimal digits a parameter (default 2) so bug "currency decimal_digits" can supply it later. - Check existing callers (C4::Overdues, Koha::Acquisition::Order, C4::Acquisition::get_rounded_price) for reliance on the old behaviour. Tests: table-driven for every mode, including negatives and exact halves. Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=23728 [Bug 23728] Rounding code should be moved to a Koha module so code can be centralised -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Blocks| |43957 Blocks| |43959 --- Comment #1 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- Created attachment 210077 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=210077&action=edit Bug 43956: Fix and unify Koha::Number::Price rounding round() used Math::BigFloat's default half-even mode and returned strings that could be "00" or "NaN", while format() rounded floats with Number::Format, so the two disagreed (0.825 gave 0.82 vs 0.83). round() now works on the decimal representation of the value, supports the modes half_even (default, as before), half_away_from_zero, half_up, half_down and half_odd plus an optional digits parameter, returns plain numbers (undef for non-numeric input); an unknown mode is logged via Koha::Logger and the default mode is used. format() and format_for_editing() round through the same engine, so they now also round half-even by default (-2.125 formats as -2.12). To test: 1. Apply the patch and run: prove t/Number/Price.t t/db_dependent/Prices.t t/db_dependent/Budgets.t 2. Confirm all pass, and that the new subtests in t/Number/Price.t fail without the patch. Visible change for librarians (prices that fall exactly half way between two cents, such as 0.125 or 2.675, are now rounded to the even cent every time, and the display agrees with the rounding used in calculations): 3. Set CurrencyFormat to US and make sure a currency is active. 4. Acquisitions > a vendor > new basket > add an order line manually. Use a quantity of 1, vendor price 10.125 and a second line with vendor price 10.135. 5. View the basket and the order lines. Prices should display as 10.12 and 10.14 (round half to even). Before the patch the display could differ from the value used when comparing prices (for example 10.125 displaying as 10.13). 6. Repeat step 5 with CurrencyFormat set to FR and check the separator changes (10,12) but the rounded value is the same. 7. Receive a shipment for one of the lines using an actual cost with three decimals, and check the invoice shows the same two decimal value as the basket would. 8. Sanity check: ordinary prices (3, 12.50, 1234567.89) display exactly as before. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43957 [Bug 43957] Add per-currency decimal digits and configurable rounding policy preferences https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43959 [Bug 43959] Cash-up reports false surplus or deficit when amounts contain fractions of a penny -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |Needs Signoff Assignee|koha-bugs@lists.koha-commun |martin.renvoize@openfifth.c |ity.org |o.uk -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Patch complexity|--- |Small patch Sponsorship status|--- |Sponsored Target Milestone|--- |27.05 Severity|enhancement |normal Initiative type|--- |Feature -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Blocks|23728 | Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=23728 [Bug 23728] [OMNIBUS] Currency precision and rounding: central engine, per-currency digits, configurable policy -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Blocks|43959 | Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43959 [Bug 43959] Cash-up reports false surplus or deficit when amounts contain fractions of a penny -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #210077|0 |1 is obsolete| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 --- Comment #2 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- Created attachment 210086 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=210086&action=edit Bug 43956: Fix and unify Koha::Number::Price rounding -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43956 --- Comment #3 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- I have replaced the patch with a revised version. Nothing has been tested yet, so I have squashed the change into the single patch rather than adding a follow-up. What changed: rounding mode and number of decimal places are now optional settings of the Koha::Number::Price object instead of parameters on each method: Koha::Number::Price->new( $value, { mode => 'half_up', digits => 3 } ) Anything not set falls back to half_even and 2 decimal places. round(), format() and format_for_editing() all use the same settings, so they cannot disagree. This gives the follow-up bugs one place to supply the currency and rounding preferences (bug 43957). The tests now also check, for every rounding mode, that format() and format_for_editing() give the same value as round(). -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org