]> wagnertech.de Git - kivitendo-erp.git/blobdiff - SL/Form.pm
Merge von 581,589-595 aus unstable: Patch zum Rundungsfehler
[kivitendo-erp.git] / SL / Form.pm
index 25886980a2b3137b7b2f1eb1c88a41c9ad05df9a..d6cbc0b7c792f266c9401957c02349a48aad4c87 100644 (file)
@@ -540,19 +540,19 @@ sub format_amount {
         $amount =~ s/\d{3,}?/$&,/g;
         $amount =~ s/,$//;
         $amount = join '', reverse split //, $amount;
-        $amount .= "\.$dec".$fillup;
+        $amount .= "\.$dec".$fillup if ($places ne '' && $places*1 != 0);
       }
 
       if ($myconfig->{numberformat} eq '1.000,00') {
         $amount =~ s/\d{3,}?/$&./g;
         $amount =~ s/\.$//;
         $amount = join '', reverse split //, $amount;
-        $amount .= ",$dec" .$fillup;
+        $amount .= ",$dec".$fillup if ($places ne '' && $places*1 != 0);
       }
 
       if ($myconfig->{numberformat} eq '1000,00') {
         $amount = "$whole";
-        $amount .= ",$dec" .$fillup;
+        $amount .= ",$dec" .$fillup if ($places ne '' && $places*1 != 0);
       }
 
       if ($dash =~ /-/) {
@@ -604,26 +604,21 @@ sub round_amount {
   $main::lxdebug->enter_sub();
 
   my ($self, $amount, $places) = @_;
-  my $rc;
+  my $round_amount;
 
-  #  $places = 3 if $places == 2;
-
-  if (($places * 1) >= 0) {
-
-    # add 1/10^$places+3
-    $rc =
-      sprintf("%.${places}f",
-              $amount + (1 / (10**($places + 3))) * (($amount > 0) ? 1 : -1));
-  } else {
-    $places *= -1;
-    $rc =
-      sprintf("%.f", $amount / (10**$places) + (($amount > 0) ? 0.1 : -0.1)) *
-      (10**$places);
-  }
+  # Rounding like "Kaufmannsrunden"
+  # Descr. http://de.wikipedia.org/wiki/Rundung
+  # Inspired by 
+  # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
+  # Solves Bug: 189
+  # Udo Spallek
+  $amount       = $amount * (10 ** ($places));
+  $round_amount = int($amount + .5 * ($amount <=> 0))/(10**($places));
 
   $main::lxdebug->leave_sub();
 
-  return $rc;
+  return $round_amount;
+  
 }