From 8edb2ea1ebaa8cb4961aba4e9b5dd96c0191d338 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Thu, 20 Sep 2012 18:12:17 +0200 Subject: [PATCH] format_amount bug: Bei places == 0 wurden trailing 0 abgeschnitten --- SL/Form.pm | 2 +- t/form/format_amount.t | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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 -- 2.20.1