From bf19eeda5d339c95bde5334727bd9a6802375a8d Mon Sep 17 00:00:00 2001 From: "G. Richardson" Date: Mon, 26 Oct 2015 09:55:59 +0100 Subject: [PATCH] format_amount - negative 0 vermeiden MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Ist eine Zahl z.B. -0.002, und wird auf 2 Stellen gerundet, so wird erst das Minuszeichen gemerkt, dann gerundet (0.00), und schießlich wird das Minus wieder hinzugefügt, dadurch bekommt man -0.00. Mit diesem Patch wird das Minus-Zeichen entfernt wenn die gerundete Zahl genau 0 ergibt. --- SL/Form.pm | 1 + t/form/format_amount.t | 2 ++ 2 files changed, 3 insertions(+) diff --git a/SL/Form.pm b/SL/Form.pm index 9045d78ca..35b21d5a2 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -822,6 +822,7 @@ sub format_amount { my $force_places = defined $places && $places >= 0; $amount = $self->round_amount($amount, abs $places) if $force_places; + $neg = 0 if $amount == 0; # don't show negative zero $amount = sprintf "%.*f", ($force_places ? $places : 10), abs $amount; # 6 is default for %fa # before the sprintf amount was a number, afterwards it's a string. because of the dynamic nature of perl diff --git a/t/form/format_amount.t b/t/form/format_amount.t index 229c88cd5..8de39a2e3 100644 --- a/t/form/format_amount.t +++ b/t/form/format_amount.t @@ -51,6 +51,8 @@ is($::form->format_amount($config, 10, -2), '10,00' , 'autotrim does not harm in is($::form->format_amount($config, 10, 0), '10', 'autotrim does not harm integers 4'); is($::form->format_amount($config, 0, 0), '0' , 'trivial zero'); +is($::form->format_amount($config, -0.002, 2), '0,00' , 'negative zero'); +is($::form->format_amount($config, -0.002, 3), '-0,002' , 'negative zero'); # dash stuff -- 2.20.1