Type conversion. Pascal's Round and Trunc Functions

When working in Pascal with variables of different types, it is often necessary to deal with the fact that when compiling a program errors occur that indicate incorrect conversion of values. For example, you cannot set an integer variable to 5.9, as this will lead to a compilation error. In this case, we have to talk about using the Round and Trunc functions in Pascal, with which you can convert the types of arguments and then perform certain tasks with them.

trunc in pascal




Type Conversion Overview

( ) - . . , , , .

Pascal

"" , .

"" , , - . Trunc "" Round, .





, , , .

, , .

trunc function in pascal




Trunc

. Trunc "" , . , Trunc (1.73) 1.

: Trunc (x:real): Longint.

Round

. Round . , Round(1.73) 2, Round (1.11) 1.

: Round (x:real): Longint.

It is worth noting that there are limitations to the result of executing the Round and Trunc functions in Pascal. Execution will fail if this result is outside the scope of Longint type values.

Obviously, the syntax of both built-in functions is quite simple and allows you to use Round and Trunc in Pascal to explicitly convert types without unnecessary troubles and does not cause compilation errors about type violation.




All Articles