Difference between revisions of "Round"
From Free Pascal wiki
Jump to navigationJump to searchm |
(.5 uses "bankers rounding", example) |
||
Line 1: | Line 1: | ||
− | + | 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 | + | '''Declaration:''' |
function Round(X: Real): Longint; | function Round(X: Real): Longint; | ||
+ | |||
+ | <syntaxhighlight> | ||
+ | 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. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | '''Output:'''<br/> | ||
+ | 9<br/> | ||
+ | 8<br/> | ||
+ | 2<br/> | ||
+ | 4<br/> | ||
See also: | See also: | ||
* [[Int]] | * [[Int]] | ||
* [[Trunc]] | * [[Trunc]] | ||
+ | * [[Div]] | ||
[[Category:Pascal]] | [[Category:Pascal]] |
Revision as of 10:56, 20 August 2016
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;
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: