Difference between revisions of "Trunc"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
 
{{Trunc}}
 
{{Trunc}}
  
Function '''Trunc''' truncates a [[Real|real]]-type value to an [[Integer]]-type value.
+
The [[RTL]] [[System unit]] contains [[Function|function]] '''trunc''' which truncates a [[Real|real]]-type value to an [[Integer]]-type value.
X is a real-type expression. Trunc returns a [[Longint]] value that is the value of X rounded toward zero.
+
It's input parameter is a real-type [[expression]].Trunc returns a [[Longint]] value that is the value of the input parameter rounded toward zero.
  
Declaration
+
 
<syntaxhighlight>
+
== Declaration ==
function Trunc(X: Real): Longint;
+
 
 +
<syntaxhighlight lang="pascal">
 +
function Trunc(X: Real): Longint;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
See also:
+
== Example Usage ==
* [[Frac]]
+
 
* [[Int]]
+
<syntaxhighlight lang="pascal">
* [[Round]]
+
begin
 +
  WriteLn( Trunc(8.7) );
 +
  WriteLn( Trunc(8.3) );
 +
  WriteLn( Trunc(-8.7) );
 +
  WriteLn( Trunc(-8.3) );
 +
 
 +
end;
 +
</syntaxhighlight>
 +
 
 +
=== Output ===
 +
8
 +
8
 +
-8
 +
-8
 +
 
 +
== See also ==
  
[[Category:Pascal]]
+
* [[Round|<syntaxhighlight lang="pascal" enclose="none">round</syntaxhighlight>]]
 +
* {{Doc|package=RTL|unit=math|identifier=ceil|text=<syntaxhighlight lang="pascal" enclose="none">math.ceil</syntaxhighlight>}} - round up
 +
* {{Doc|package=RTL|unit=math|identifier=floor|text=<syntaxhighlight lang="pascal" enclose="none">math.floor</syntaxhighlight>}} - round down
 +
* [[Frac|<syntaxhighlight lang="pascal" enclose="none">frac</syntaxhighlight>]] - returns the fractional part of a floating point value 
 +
* [[Int|<syntaxhighlight lang="pascal" enclose="none">int</syntaxhighlight>]] - returns the integer part of a floating point value
 +
* [[Div|<syntaxhighlight lang="pascal" enclose="none">div</syntaxhighlight>]] - integer division
 +
* [[Comparison of approaches for rounding to an integer]]

Revision as of 21:03, 3 September 2019

English (en) suomi (fi) русский (ru)

The RTL System unit contains function trunc which truncates a real-type value to an Integer-type value. It's input parameter is a real-type expression.Trunc returns a Longint value that is the value of the input parameter rounded toward zero.


Declaration

function Trunc(X: Real): Longint;

Example Usage

begin
   WriteLn( Trunc(8.7) );
   WriteLn( Trunc(8.3) );
   WriteLn( Trunc(-8.7) );
   WriteLn( Trunc(-8.3) );

end;

Output

8
8
-8
-8

See also