X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FForm.pm;h=539cd9cb24f6b488592e1460bfe4cc528f2e312a;hb=c7ad671d22e558e157797af7d787c8df499be1b4;hp=55ee46d4cb453170eb1da4a18fcbbc22007dc410;hpb=4fa7102b36468079313df9e579f7537d49aab859;p=kivitendo-erp.git diff --git a/SL/Form.pm b/SL/Form.pm index 55ee46d4c..539cd9cb2 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -866,41 +866,30 @@ sub format_amount { $main::lxdebug->enter_sub(2); my ($self, $myconfig, $amount, $places, $dash) = @_; - $dash ||= ''; + $amount ||= 0; + $dash ||= ''; + my $neg = $amount < 0; + my $force_places = defined $places && $places >= 0; - if ($amount eq "") { - $amount = 0; - } - - $amount *= 1; - - # Hey watch out! The amount can be an exponential term like 1.13686837721616e-13 + $amount = $self->round_amount($amount, abs $places) if $force_places; + $amount = sprintf "%.*f", ($force_places ? $places : 10), abs $amount; # 6 is default for %fa - my $neg = ($amount =~ s/^-//); - my $exp = ($amount =~ m/[e]/) ? 1 : 0; + # before the sprintf amount was a number, afterwards it's a string. because of the dynamic nature of perl + # 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. - if (defined($places) && ($places ne '')) { - if (not $exp) { - if ($places < 0) { - $amount *= 1; - $places *= -1; - - if ($amount =~ /\.(\d+)/) { - my $actual_places = length $1; - $places = $actual_places if $actual_places > $places; - } - } - } - $amount = $self->round_amount($amount, $places); - } + $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 - - $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters + my @p = split(/\./, $amount); # split amount at decimal point + $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters $amount = $p[0]; - $amount .= $d[0].($p[1]||'').(0 x ($places - length ($p[1]||''))) if ($places || $p[1] ne ''); + if ($places || $p[1]) { + $amount .= $d[0] + . ( $p[1] || '' ) + . (0 x (abs($places || 0) - length ($p[1]||''))); # pad the fraction + } $amount = do { ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) : @@ -908,7 +897,6 @@ sub format_amount { ($neg ? "-$amount" : "$amount" ) ; }; - $main::lxdebug->leave_sub(2); return $amount; }