Difference between revisions of "Round/de"
From Lazarus wiki
Jump to navigationJump to searchLine 42: | Line 42: | ||
var | var | ||
i: integer; | i: integer; | ||
− | x: single; | + | x: single = 0.5; |
begin | begin | ||
− | |||
− | |||
for i := 0 to 99 do begin | for i := 0 to 99 do begin | ||
Canvas.Pixels[round(x), 20]:= $00; | Canvas.Pixels[round(x), 20]:= $00; | ||
− | WriteLn('Input:', x:6:2, ' round: ', Round(x): 4); | + | WriteLn('Input:', x: 6: 2, ' round: ', Round(x): 4); |
x := x + 1.0; | x := x + 1.0; | ||
end; | end; | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | Als Alternative [[Trunc]] verwenden. | ||
===Ausgabe:=== | ===Ausgabe:=== |
Revision as of 17:19, 29 May 2018
│
Deutsch (de) │
English (en) │
Esperanto (eo) │
suomi (fi) │
русский (ru) │
Rundet eine Fliesskommazahl auf eine Ganzzahl.
Round
Deklaration:
function Round(X: Real): Longint;
Beispiel:
Code:
var
i1, i2: Integer;
begin
WriteLn( Round(8.7) );
WriteLn( Round(8.3) );
// Beispiele für "Banker-Runden" - .5 wird auf die nächste gerade Zahl eingestellt
WriteLn( Round(2.5) );
WriteLn( Round(3.5) );
i := Round(12.50); // Rundet ab
WriteLn(i);
i := Round(12.51); // Rundet auf
WriteLn(i);
end.
Ausgabe:
9 8 2 4 1 13
Gefährliche Falle
Dank des Banker-Runden, zeichnet folgendes Beispiel keine durchgehende Linie, sondern eine Gepunktete.
Code:
var
i: integer;
x: single = 0.5;
begin
for i := 0 to 99 do begin
Canvas.Pixels[round(x), 20]:= $00;
WriteLn('Input:', x: 6: 2, ' round: ', Round(x): 4);
x := x + 1.0;
end;
end;
Als Alternative Trunc verwenden.
Ausgabe:
Input: 0.50 round: 0 Input: 1.50 round: 2 Input: 2.50 round: 2 Input: 3.50 round: 4 Input: 4.50 round: 4 ....
See also: