From 7915128cc09f9756439ab9216a82ada3870893c3 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 26 Oct 2015 12:08:10 +0100 Subject: [PATCH] round_amount: Anzahl Stellen nicht zu hoch werden lassen --- SL/Form.pm | 4 +++- t/form/round_amount.t | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/SL/Form.pm b/SL/Form.pm index 35b21d5a2..c59669c41 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -982,7 +982,9 @@ sub round_amount { # part. If an overflow occurs then apply that overflow to the part # before the decimal sign as well using integer arithmetic again. - my $amount_str = sprintf '%.*f', $places + 10, abs($amount); + my $int_amount = int(abs $amount); + my $str_places = max(min(10, 16 - length("$int_amount") - $places), $places); + my $amount_str = sprintf '%.*f', $places + $str_places, abs($amount); return $amount unless $amount_str =~ m{^(\d+)\.(\d+)$}; diff --git a/t/form/round_amount.t b/t/form/round_amount.t index d9c8edbb7..18553c0f4 100644 --- a/t/form/round_amount.t +++ b/t/form/round_amount.t @@ -57,6 +57,8 @@ is($::form->round_amount(198.90 * 0.75, 2), '149.18', '198.90 * 0.75 @ 2'); is($::form->round_amount(198.90 * 0.75, 1), '149.2', '198.90 * 0.75 @ 1'); is($::form->round_amount(198.90 * 0.75, 0), '149', '198.90 * 0.75 @ 0'); +is($::form->round_amount(19610.975, 2), '19610.98', '19610.975 @ 2'); + # Negative values is($::form->round_amount(-1.05, 2), '-1.05', '-1.05 @ 2'); is($::form->round_amount(-1.05, 1), '-1.1', '-1.05 @ 1'); -- 2.20.1