@@ -, +, @@ tests --- C4/Koha.pm | 34 ++++++++++++++++++++++++++++++++++ C4/Overdues.pm | 2 +- t/Koha.t | 14 +++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) --- a/C4/Koha.pm +++ a/C4/Koha.pm @@ -1310,6 +1310,40 @@ sub IsKohaFieldLinked { return $is_linked->[0]; } +=head2 compareNumbers + + As floating point numbers may be rounded differently and + inconsistently, this function converts them to strings of + a given accuracy for the comparison. + + my $number1 = 5.60000; + my $number2 = 5.6; + if (C4::Koha::compareNumbers($number1,$number2,2) { + print "The two numbers match.\n"; + } else { + print "The two numbers do not match.\n"; + } + + Return 1 if the numbers match. + +=cut + +sub compareNumbers { + my ($n1,$n2,$accuracy) = @_; + if (defined $n1 && defined $n2) { + if (!defined $accuracy) { + $accuracy = 2; + } + my $format1 = "%.${accuracy}f"; + my $format2 = $format1; + return (sprintf($format1,$n1) eq sprintf($format2,$n2)); + } elsif (!defined $n1 && !defined $n2) { + return 1; + } else { + return 0; + } +} + 1; __END__ --- a/C4/Overdues.pm +++ a/C4/Overdues.pm @@ -575,7 +575,7 @@ sub UpdateFine { # we're updating an existing fine. Only modify if amount changed # Note that in the current implementation, you cannot pay against an accruing fine # (i.e. , of accounttype 'FU'). Doing so will break accrual. - if ( $data->{'amount'} ne sprintf('%.6f', $amount) ) { + if ( C4::Koha::compareNumbers($data->{'amount'},$amount,6) ) { my $accountline = Koha::Account::Lines->find( $data->{accountlines_id} ); my $diff = $amount - $data->{'amount'}; --- a/t/Koha.t +++ a/t/Koha.t @@ -25,7 +25,7 @@ use Module::Load::Conditional qw/check_install/; BEGIN { if ( check_install( module => 'Test::DBIx::Class' ) ) { - plan tests => 38; + plan tests => 47; } else { plan skip_all => "Need Test::DBIx::Class" } @@ -155,4 +155,16 @@ ok($@ eq '', 'NormalizeISSN does not throw exception when parsing invalid ISSN') is($issns[0], 'abc', 'Original ISSN passed through even if invalid'); is(scalar(@issns), 1, 'zero additional variations returned of invalid ISSN'); +ok( C4::Koha::compareNumbers(1,1,2),'1 matches 1 as expected'); +ok( C4::Koha::compareNumbers(1.25,1.251,2),'1.25 matches 1.251 with 2 decimal accuracy'); + +ok(!C4::Koha::compareNumbers(1,2,2),'1 does not match 2 as expected'); +ok(!C4::Koha::compareNumbers(undef,1 ,2),'undef does not match 1 as expected'); +ok(!C4::Koha::compareNumbers(1,undef,2),'1 does not match undef as expected'); + +ok(C4::Koha::compareNumbers( 5.600000 , 5.6 ),'number-number test'); +ok(C4::Koha::compareNumbers( 5.600000 ,'5.6'),'number-string test'); +ok(C4::Koha::compareNumbers('5.600000', 5.6 ),'string-number test'); +ok(C4::Koha::compareNumbers('5.600000','5.6'),'string-string test'); + 1; --