Difference between revisions of "Round"

From Lazarus wiki
Jump to navigationJump to search
m
m (Fixed syntax highlighting; wiki markup)
Line 4: Line 4:
 
It's input parameter is a real-type expression and Round returns a [[Longint]] value that is the value of the input rounded to the nearest whole number. If the input value is exactly halfway between two whole numbers - N.5 - then "bankers rounding" is used, with the result being the nearest even number.
 
It's input parameter is a real-type expression and Round returns a [[Longint]] value that is the value of the input rounded to the nearest whole number. If the input value is exactly halfway between two whole numbers - N.5 - then "bankers rounding" is used, with the result being the nearest even number.
  
'''Declaration:'''
+
== Declaration ==
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
function Round(X: Real): Longint;
 
function Round(X: Real): Longint;
 
</syntaxhighlight>
 
</syntaxhighlight>
<br>
+
 
<syntaxhighlight>
+
== Example Usage ==
 +
 
 +
<syntaxhighlight lang="pascal">
 
begin
 
begin
 
   WriteLn( Round(8.7) );
 
   WriteLn( Round(8.7) );
Line 19: Line 22:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''Output:'''<br/>
+
=== Output ===
<pre>
+
 
9
+
  9
8
+
  8
2
+
  2
4
+
  4
</pre>
+
 
 +
== See also ==
  
See also:
 
 
* [[Int]]
 
* [[Int]]
 
* [[Trunc]]
 
* [[Trunc]]

Revision as of 01:19, 10 August 2019

Deutsch (de) English (en) Esperanto (eo) suomi (fi) русский (ru)

The RTL System unit contains function Round, which rounds a Real-type value to an Integer-type value. It's input parameter is a real-type expression and Round returns a Longint value that is the value of the input rounded to the nearest whole number. If the input value is exactly halfway between two whole numbers - N.5 - then "bankers rounding" is used, with the result being the nearest even number.

Declaration

function Round(X: Real): Longint;

Example Usage

begin
   WriteLn( Round(8.7) );
   WriteLn( Round(8.3) );
   // examples of "bankers rounding" - .5 is adjusted to the nearest even number
   WriteLn( Round(2.5) );
   WriteLn( Round(3.5) );
end.

Output

 9
 8
 2
 4

See also