Leser: 41
1
2
3
4
use Math::BigFloat;
my $wert = Math::BigFloat->new(0.045);
my $gerundet = $wert->fround(2);
print "Gerundet: >$gerundet<\n";
Quotefround ( +$scale )
Preserves accuracy to $scale digits from the left (aka significant digits) and pads the rest with zeros. If the number is between 1 and -1, the significant digits count from the first non-zero after the '.'
QuoteReturns the integer portion of EXPR. If EXPR is omitted, uses
$_. You should not use this function for rounding: one because
it truncates towards 0, and two because machine representations
of floating point numbers can sometimes produce counterintuitive
results. For example, "int(-6.725/0.025)" produces -268 rather
than the correct -269; that's because it's really more like
-268.99999999999994315658 instead. Usually, the "sprintf",
"printf", or the "POSIX::floor" and "POSIX::ceil" functions will
serve you better than will int().
1
2
3
4
my $betrag = abs(int(0.57 * 100)) - int(0 * 100);
my $neu_betrag = sprintf("%d",0.57 * 100) - sprintf("%d",0 * 100);
print "Betrag: >$betrag<\n";
print "Neu : >$neu_betrag<\n";
1
2
3
use POSIX;
my $pos_betrag = floor((0.57 * 100) - (0 * 100));
print "POSIX : >$pos_betrag<\n";
QuotePOSIX : >56<
QuotePOSIX : >56<
QuoteFrage 1: Wie kann ich erreichen, dass ich bei der Multiplikation eines Wertes mit max. zwei Nachkommastellen anschließend einen Wert ohne jegliche Nachkommastellen bekomme?
1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use warnings;
for my $betrag ( qw/345.4 .34 123,456.45 .2/ ) {
print "$betrag => ";
my( $euro, $cent ) = split /\./, $betrag;
$euro = '0' unless $euro;
$euro =~ tr/,//d;
$cent .= '0' while length $cent < 2;
my $gesamt_cent = $euro . $cent;
$gesamt_cent =~ s/^0+//;
print "$gesamt_cent\n";
}
2009-07-06T17:19:49 mikdoeAlles ganz toll und ich flippe gleich aus.
...
Bitte um HIIIIILLFFEEEEE!
Wieso kann der PC nicht rechnen????
Quote...that's because it's really more like
-268.99999999999994315658 instead...
int(.57 * 100)
2009-07-07T19:17:44 mikdoeUnd was muss ich anders machen?
2009-07-07T16:32:18 pqwenn du nur mit integern rechnest, kannst du ja niemals auf .57 kommen.
QuoteRounding in financial applications can have serious implications, and
the rounding method used should be specified precisely. In these
cases, it probably pays not to trust whichever system rounding is being
used by Perl, but to instead implement the rounding function you need
yourself.
To see why, notice how you'll still have an issue on half-way-point
alternation:
for ($i = 0; $i < 1.01; $i += 0.05) { printf "%.1f ",$i}
0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7
0.8 0.8 0.9 0.9 1.0 1.0
Don't blame Perl. It's the same as in C. IEEE says we have to do
this. Perl numbers whose absolute values are integers under 2**31 (on
32 bit machines) will work pretty much like mathematical integers.
Other numbers are not guaranteed.
2009-07-06T14:35:01 mikdoeIrgendwie raff ich es nicht.
Code: (dl )1
2
3
4use Math::BigFloat;
my $wert = Math::BigFloat->new(0.045);
my $gerundet = $wert->fround(2);
print "Gerundet: >$gerundet<\n";
Bringt leider NICHT 0.05 wie ich es gern hätte.
Was mache ich falsch? Sehe gerade den Wald vor lauter Bäumen nicht mehr.
Danke und Grüße
1 2 3
my $z = Math::BigFloat->new('0.045'); $z->ffround(-2,'common'); print $z->bstr;
perldoc Math::BigFloatAll rounding functions take as a second parameter a rounding mode from one of the following: 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'.
perldoc Math::BigIntRounding 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.