From: Sven Schöling Date: Thu, 20 Sep 2012 16:12:17 +0000 (+0200) Subject: format_amount bug: Bei places == 0 wurden trailing 0 abgeschnitten X-Git-Tag: release-3.0.0beta1~230 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=8edb2ea1ebaa8cb4961aba4e9b5dd96c0191d338;hp=7d29de1cc9bf5d7547f387441b72f80b0edba0fe;p=kivitendo-erp.git format_amount bug: Bei places == 0 wurden trailing 0 abgeschnitten --- diff --git a/SL/Form.pm b/SL/Form.pm index ff7d0efa0..539cd9cb2 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -878,7 +878,7 @@ sub format_amount { # this is easy to confuse, so keep in mind: before this comment no s///, m//, concat or other strong ops on # $amount. after this comment no +,-,*,/,abs. it will only introduce subtle bugs. - $amount =~ s/0*$//; # cull trailing 0s + $amount =~ s/0*$// unless defined $places && $places == 0; # cull trailing 0s my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars my @p = split(/\./, $amount); # split amount at decimal point diff --git a/t/form/format_amount.t b/t/form/format_amount.t index 58a7c17c8..229c88cd5 100644 --- a/t/form/format_amount.t +++ b/t/form/format_amount.t @@ -32,6 +32,7 @@ is($::form->format_amount($config, 1.00045, -5), '1.00045', 'negative places 2') is($::form->format_amount($config, 1, -2), '1.00', 'negative places 3'); # bugs amd edge cases +$config->{numberformat} = '1.000,00'; is($::form->format_amount({ numberformat => '1.000,00' }, 0.00005), '0,00005', 'messing with small numbers and no precision'); is($::form->format_amount({ numberformat => '1.000,00' }, undef), '0', 'undef'); @@ -44,6 +45,12 @@ is($::form->format_amount($config, -0.545, 0), '-1', 'neg rounding up with preci is($::form->format_amount($config, 1.00), '1', 'autotrim to 0 places'); +is($::form->format_amount($config, 10), '10', 'autotrim does not harm integers'); +is($::form->format_amount($config, 10, 2), '10,00' , 'autotrim does not harm integers 2'); +is($::form->format_amount($config, 10, -2), '10,00' , 'autotrim does not harm integers 3'); +is($::form->format_amount($config, 10, 0), '10', 'autotrim does not harm integers 4'); + +is($::form->format_amount($config, 0, 0), '0' , 'trivial zero'); # dash stuff