From 6d04095101e8cf05d3bda6ed8cf639ad6b779892 Mon Sep 17 00:00:00 2001 From: Raphael Straub Date: Thu, 8 Feb 2024 14:17:56 +0000 Subject: [PATCH] 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 --- Koha/Number/Price.pm | 8 ++------ t/Number/Price.t | 5 ++++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Koha/Number/Price.pm b/Koha/Number/Price.pm index e9a6aa748e..a0f847d495 100644 --- a/Koha/Number/Price.pm +++ b/Koha/Number/Price.pm @@ -19,6 +19,7 @@ package Koha::Number::Price; use Modern::Perl; +use Math::BigFloat; use Number::Format; use C4::Context; use Koha::Acquisition::Currencies; @@ -83,12 +84,7 @@ sub round { my $format_params = $self->_format_params; - # To avoid the system to crash, we will not format big number - # We divide per 100 because we want to keep the default DECIMAL_DIGITS (2) - # error - round() overflow. Try smaller precision or use Math::BigFloat - return $self->value if $self->value > Number::Format::MAX_INT/100; - - return Number::Format->new(%$format_params)->round($self->value); + return Number::Format->new(%$format_params)->round( Math::BigFloat->new( $self->value ) )->bdstr(); } sub _format_params { diff --git a/t/Number/Price.t b/t/Number/Price.t index ce607a83a1..9017fe793e 100755 --- a/t/Number/Price.t +++ b/t/Number/Price.t @@ -1,6 +1,6 @@ use Modern::Perl; -use Test::More tests => 35; +use Test::More tests => 36; use Test::MockModule; use t::lib::Mocks; @@ -136,6 +136,9 @@ is( Koha::Number::Price->new(3)->unformat, '3', 'CHF: unformat 3' ); is( Koha::Number::Price->new(1234567890)->unformat, '1234567890', 'CHF: unformat 1234567890' ); +# Rounding +is( Koha::Number::Price->new(17.955)->round, '17.96', 'Round 17.955' ); + subtest 'Changes for format' => sub { # See also bug 18736 plan tests => 3; -- 2.30.2