Thread Kaufm. Runden (34 answers)
Opened by mikdoe at 2009-07-06 16:35

MatthiasW
 2009-07-08 10:46
#122958 #122958
User since
2008-01-27
367 Artikel
BenutzerIn
[default_avatar]
perldoc Math::BigFloat
All rounding functions take as a second parameter a rounding mode from one of the following: 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'.

Näheres zu den diversen Rundungsmodi findet man in der Dokumentation von Math::BigInt:
perldoc Math::BigInt
Rounding mode R
When rounding a number, different 'styles' or 'kinds' of rounding are
possible. (Note that random rounding, as in Math::Round, is not
implemented.)

'trunc'
truncation invariably removes all digits following the rounding place,
replacing them with zeros. Thus, 987.65 rounded to tens (P=1) becomes
980, and rounded to the fourth sigdig becomes 987.6 (A=4). 123.456
rounded to the second place after the decimal point (P=-2) becomes
123.46.

All other implemented styles of rounding attempt to round to the
"nearest digit." If the digit D immediately to the right of the
rounding place (skipping the decimal point) is greater than 5, the
number is incremented at the rounding place (possibly causing a
cascade of incrementation): e.g. when rounding to units, 0.9 rounds to
1, and -19.9 rounds to -20. If D < 5, the number is similarly
truncated at the rounding place: e.g. when rounding to units, 0.4
rounds to 0, and -19.4 rounds to -19.

However the results of other styles of rounding differ if the digit
immediately to the right of the rounding place (skipping the decimal
point) is 5 and if there are no digits, or no digits other than 0,
after that 5. In such cases:

'even'
rounds the digit at the rounding place to 0, 2, 4, 6, or 8 if it is
not already. E.g., when rounding to the first sigdig, 0.45 becomes
0.4, -0.55 becomes -0.6, but 0.4501 becomes 0.5.

'odd'
rounds the digit at the rounding place to 1, 3, 5, 7, or 9 if it is
not already. E.g., when rounding to the first sigdig, 0.45 becomes
0.5, -0.55 becomes -0.5, but 0.5501 becomes 0.6.

'+inf'
round to plus infinity, i.e. always round up. E.g., when rounding to
the first sigdig, 0.45 becomes 0.5, -0.55 becomes -0.5, and 0.4501
also becomes 0.5.

'-inf'
round to minus infinity, i.e. always round down. E.g., when rounding
to the first sigdig, 0.45 becomes 0.4, -0.55 becomes -0.6, but 0.4501
becomes 0.5.

'zero'
round to zero, i.e. positive numbers down, negative ones up. E.g.,
when rounding to the first sigdig, 0.45 becomes 0.4, -0.55 becomes
-0.5, but 0.4501 becomes 0.5.

'common'
round up if the digit immediately to the right of the rounding place
is 5 or greater, otherwise round down. E.g., 0.15 becomes 0.2 and
0.149 becomes 0.1.

Demnach ist 'common' das was du hier benötigst. Als Standard-Runde-Modus verwendet Math::BigFloat 'trunc', deshalb bekamst du ein "falsches" Ergebnis. Den Standard-Runde-Modus kannst du übrigens mittels Math::BigFloat->round_mode($round_mode) ändern.

MfG
perl -E'*==*",s;;%ENV=~m,..$,,$&+42;e,$==f;$"++for+ab..an;@"=qw,u t,,print+chr;sub f{split}say"@{=} me"'

View full thread Kaufm. Runden