Thread Kaufmännisch korrekt runden
(67 answers)
Opened by bianca at 2009-12-11 07:14 2009-12-11T20:23:16 topeg Bei zwei Stellen ist 0.06 beim ersten und 0.05 beim zweiten als Ergebnis auch völlig korrekt. Wir sprechen ja von kaufmännischem Runden. Habe dennoch meine Testumgebung etwas erneuert: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 my %test = ( '0.05499999999999999334,3' => '0.055', '-0.05499999999999999334,3' => '-0.055', '0.05499999999999999334,2' => '0.05', '-0.05499999999999999334,2' => '-0.05', '0.055,2' => '0.06', '-0.055,2' => '-0.06', '2132343.565,2' => '2132343.57', '-2132343.565,2' => '-2132343.57', '0.2225000000,3' => '0.223', '-0.2225000000,3' => '-0.223', '0.2224000000,3' => '0.222', '-0.2224000000,3' => '-0.222', '0.05,1' => '0.1', '-0.05,1' => '-0.1', '0.005,2' => '0.01', '-0.005,2' => '-0.01', '0.1005,3' => '0.101', '-0.1005,3' => '-0.101', '0.0105,3' => '0.011', '-0.0105,3' => '-0.011', '0.105,2' => '0.11', '-0.105,2' => '-0.11', '0.04,1' => '0', '-0.04,1' => '0', '0.004,2' => '0', '-0.004,2' => '0', (.57 * 100) . ',2' => '57', (-.57 * 100) . ',2' => '-57', '1.295,2' => '1.3', '-1.295,2' => '-1.3', '9.999,3' => '9.999', '-9.999,3' => '-9.999', '9.999,2' => '10', '-9.999,2' => '-10', '9.999,1' => '10', '-9.999,1' => '-10', '9.999,0' => '10', '-9.999,0' => '-10', '8.9964,2' => '9', '-8.9964,2' => '-9', '8.9964,0' => '9', '-8.9964,0' => '-9', '8.9964,1' => '9', '-8.9964,1' => '-9', '1,2' => '1', '-1,2' => '-1', '1.455,2' => '1.46', '-1.455,2' => '-1.46', '1.9,0' => '2', '-1.9,0' => '-2', '12345.34,1' => '12345.3', '-12345.34,1' => '-12345.3', '12345.35,1' => '12345.4', '-12345.35,1' => '-12345.4', '12.2345678905,9' => '12.234567891', '-12.2345678905,9' => '-12.234567891', '.5678,3' => '0.568', '-.5678,3' => '-0.568', '4.24306121591708e-007,2' => '0', '1e+100,3' => '1e+100', '.5674,3' => '0.567', '-.5674,3' => '-0.567', '.5670,3' => '0.567', '-.5670,3' => '-0.567', '456.4,0' => '456', '-456.4,0' => '-456', '456.5,0' => '457', '-456.5,0' => '-457', '0.49999999,0' => '0', '-0.49999999,0' => '0', '0.999999999,0' => '1', '-0.999999999,0' => '-1', '0.5,0' => '1', '-0.5,0' => '-1', '999999999999.999,2' => '1000000000000', '-999999999999.999,2' => '-1000000000000', '999999999999.994,2' => '999999999999.99', '-999999999999.994,2' => '-999999999999.99', '00000034.999,2' => '35', '-00000034.999,2' => '-35', ); my $space = '.' x 25; foreach my $test (sort {(split /,/,$a)[0] <=> (split /,/,$b)[0] } keys %test) { my ($wert,$stellen) = split /,/,$test; my $gerundet = &{$sub} ($wert,$stellen); print "\'$wert\'" . substr ($space,0,25 - length ($wert)) . "auf $stellen Stelle(n): \'$gerundet\'" . substr ($space,0,25 - length ($gerundet)) . ($gerundet eq $test{$test} ? ' OK' : " FEHLER! Erwartet \'$test{$test}\'") . "\n"; } print "(7.56*1.19)................auf 2 Stellen : \'" . &{$sub} (7.56*1.19,2) . "\'........................ " . (&{$sub} (7.56*1.19,2) == 9 ? 'OK' : 'FEHLER! Erwartet \'9\'') . "\n"; print "(-7.56*1.19)...............auf 2 Stellen : \'" . &{$sub} (-7.56*1.19,2) . "\'....................... " . (&{$sub} (-7.56*1.19,2) == -9 ? 'OK' : 'FEHLER! Erwartet \'9\'') . "\n"; print "(.57*100)..................auf 0 Stellen : \'" . &{$sub} (.57*100,0) . "\'....................... " . (&{$sub} (.57*100,0) == 57 ? 'OK' : 'FEHLER! Erwartet \'57\'') . "\n"; print "(.57*100)..................auf 2 Stellen : \'" . &{$sub} (.57*100,2) . "\'....................... " . (&{$sub} (.57*100,2) == 57 ? 'OK' : 'FEHLER! Erwartet \'57\'') . "\n"; Und diese Tests besteht die pq-Lösung alle mit Bravur. 10 print "Hallo"
20 goto 10 |