X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/2f8ead125d69d2b91db9b1517bb6a3604ede1f6b..38044b51d435d611d602dfb1f80ea95543416ab4:/SL/Form.pm diff --git a/SL/Form.pm b/SL/Form.pm index 1368df738..9d032bf69 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -41,11 +41,13 @@ use Carp; use Data::Dumper; use Carp; +use Config; use CGI; use Cwd; use Encode; use File::Copy; use IO::File; +use Math::BigInt; use SL::Auth; use SL::Auth::DB; use SL::Auth::LDAP; @@ -950,28 +952,33 @@ sub parse_amount { sub round_amount { my ($self, $amount, $places) = @_; - # Rounding like "Kaufmannsrunden" (see http://de.wikipedia.org/wiki/Rundung ) + # We use Perl's knowledge of string representation for + # rounding. First, convert the floating point number to a string + # with a high number of places. Then split the string on the decimal + # sign and use integer calculation for rounding the decimal places + # part. If an overflow occurs then apply that overflow to the part + # before the decimal sign as well using integer arithmetic again. - # Round amounts to eight places before rounding to the requested - # number of places. This gets rid of errors due to internal floating - # point representation. - $amount = $self->round_amount($amount, 8) if $places < 8; + my $amount_str = sprintf '%.*f', $places + 10, abs($amount); - # Remember the amount's sign but calculate in positive values only. - my $sign = $amount <=> 0; - $amount = abs $amount; + return $amount unless $amount_str =~ m{^(\d+)\.(\d+)$}; - # Shift the amount left by $places+1 decimal places and truncate it - # to integer. Then to the integer equivalent of rounding to the next - # multiple of 10: first add half of it (5). Then truncate it back to - # the lower multiple of 10 by subtracting $amount modulo 10. - my $shift = 10 ** ($places + 1); - $amount = int($amount * $shift) + 5; - $amount -= $amount % 10; + my ($pre, $post) = ($1, $2); + my $decimals = '1' . substr($post, 0, $places); - # Lastly shift the amount back right by $places+1 decimal places and - # restore its sign. Then we're done. - $amount = ($amount / $shift) * $sign; + my $propagation_limit = $Config{i32size} == 4 ? 7 : 18; + my $add_for_rounding = substr($post, $places, 1) >= 5 ? 1 : 0; + + if ($places > $propagation_limit) { + $decimals = Math::BigInt->new($decimals)->badd($add_for_rounding); + $pre = Math::BigInt->new($decimals)->badd(1) if substr($decimals, 0, 1) eq '2'; + + } else { + $decimals += $add_for_rounding; + $pre += 1 if substr($decimals, 0, 1) eq '2'; + } + + $amount = ("${pre}." . substr($decimals, 1)) * ($amount <=> 0); return $amount; } @@ -2146,7 +2153,7 @@ sub _get_taxzones { $key = "all_taxzones" unless ($key); my $tzfilter = ""; - $tzfilter = "WHERE obsolete is FALSE" if $key eq 'ALL_ACTIVE_TAXZONES'; + $tzfilter = "WHERE obsolete is FALSE" if $key eq 'ALL_ACTIVE_TAXZONES'; my $query = qq|SELECT * FROM tax_zones $tzfilter ORDER BY sortkey|;