Difference between revisions of "Round/de"

From Free Pascal wiki
Jump to navigationJump to search
Line 27: Line 27:
  
 
===Ausgabe:===
 
===Ausgabe:===
9<br>
+
<pre>
8<br>
+
9
2<br>
+
8
4<br>
+
2
12<br>
+
4
13<br>
+
1
 +
13
 +
</pre>
 +
 
 +
==Gefährliche Falle==
 +
Dank des Banker-Runden, zeichnet folgendes Beispiel keine durchgehende Linie, sondern eine Gepunktete.
 +
===Code:===
 +
<syntaxhighlight>
 +
var
 +
  i: integer;
 +
  x: single;
 +
begin
 +
  x := 0.5;
 +
 
 +
  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;
 +
</syntaxhighlight>
 +
 
 +
===Ausgabe:===
 +
<pre>
 +
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
 +
....
 +
</pre>
 +
 
  
 
See also:
 
See also:

Revision as of 16:10, 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;
begin
  x := 0.5;

  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;

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: