From 523441a2cebbc6dad08474d9bcfa5b364d70263d Mon Sep 17 00:00:00 2001 From: tetragon Date: Mon, 25 Sep 2006 21:00:43 +0000 Subject: Fix and simplify rounding in round_amount using Math::BigFloat git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@149 4979c152-3d1c-0410-bac9-87ea11338e46 --- LedgerSMB/Form.pm | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/LedgerSMB/Form.pm b/LedgerSMB/Form.pm index 715ee8c4..c30d70eb 100755 --- a/LedgerSMB/Form.pm +++ b/LedgerSMB/Form.pm @@ -33,6 +33,7 @@ # #====================================================================== +use Math::BigFloat lib=>'GMP'; package Form; @@ -489,21 +490,15 @@ sub round_amount { my ($self, $amount, $places) = @_; - # $places = 4 if $places == 2; - my ($null, $dec) = split /\./, $amount; - $dec = length $dec; - $dec = ($dec > $places) ? $dec : $places; - my $adj = ($amount < 0) ? (1/10**($dec+2)) * -1 : (1/10**($dec+2)); + # These rounding rules follow from the previous implementation. + # They should be changed to allow different rules for different accounts. + Math::BigFloat->round_mode('+inf') if $amount >= 0; + Math::BigFloat->round_mode('-inf') if $amount < 0; - if (($places * 1) >= 0) { - $amount = sprintf("%.${places}f", $amount + $adj) * 1; - } else { - $places *= -1; - $amount = sprintf("%.0f", $amount); - $amount = sprintf("%.f", $amount / (10 ** $places)) * (10 ** $places); - } + $amount = Math::BigFloat->new($amount)->ffround(-$places) if $places >= 0; + $amount = Math::BigFloat->new($amount)->ffround(-($places-1)) if $places < 0; - $amount; + return $amount; } -- cgit v1.2.3