Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 7915128c

Von Moritz Bunkus vor mehr als 8 Jahren hinzugefügt

  • ID 7915128cc09f9756439ab9216a82ada3870893c3
  • Vorgänger bf19eeda
  • Nachfolger 75f43bc1

round_amount: Anzahl Stellen nicht zu hoch werden lassen

Unterschiede anzeigen:

SL/Form.pm
982 982
  # part. If an overflow occurs then apply that overflow to the part
983 983
  # before the decimal sign as well using integer arithmetic again.
984 984

  
985
  my $amount_str = sprintf '%.*f', $places + 10, abs($amount);
985
  my $int_amount = int(abs $amount);
986
  my $str_places = max(min(10, 16 - length("$int_amount") - $places), $places);
987
  my $amount_str = sprintf '%.*f', $places + $str_places, abs($amount);
986 988

  
987 989
  return $amount unless $amount_str =~ m{^(\d+)\.(\d+)$};
988 990

  
t/form/round_amount.t
57 57
is($::form->round_amount(198.90 * 0.75, 1), '149.2',  '198.90 * 0.75 @ 1');
58 58
is($::form->round_amount(198.90 * 0.75, 0), '149',    '198.90 * 0.75 @ 0');
59 59

  
60
is($::form->round_amount(19610.975, 2), '19610.98', '19610.975 @ 2');
61

  
60 62
# Negative values
61 63
is($::form->round_amount(-1.05, 2), '-1.05', '-1.05 @ 2');
62 64
is($::form->round_amount(-1.05, 1), '-1.1',  '-1.05 @ 1');

Auch abrufbar als: Unified diff