summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46>2006-09-25 21:00:43 +0000
committertetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46>2006-09-25 21:00:43 +0000
commit523441a2cebbc6dad08474d9bcfa5b364d70263d (patch)
tree6a30c4641eae88faa662aa3b66cabd8c6501e636
parentb14f9f357e177ed8e603aa594a6fcf862311cff3 (diff)
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
-rwxr-xr-xLedgerSMB/Form.pm21
1 files 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;
}