Type conversion. Functions Round and Trunc in "Pascal"
Working in Pascal with variables of different types,quite often we have to deal with the fact that when compiling the program there are errors that indicate an incorrect conversion of values. For example, you can not assign a variable of type integer to 5.9, since this will result in a compilation error. In this case, you have to talk about using the Round and Trunc functions in Pascal, with which you can convert the types of arguments and continue to perform certain tasks with them.
General information about type conversions
Type conversion (reduction of values) isprocess of converting values of one type of data to another. There are explicit and implicit casting types. The first is specified directly by the developer using either language constructs or using functions, and the second is executed independently by the interpreter or the code compiler according to the rules declared in the standard of one or another programming language.
Type Conversion in Pascal
In the Pascal programming language, you can use both explicit and implicit type conversions.
With explicit casting, Pascal uses calls to special conversion functions whose arguments belong to the same type, and the value to a completely different type of data. These are the Trunc function in Pascal and the Round function, which will be discussed in more detail below.
Implicit casting of types in this language is possible only in cases where in expressions that consist of integer and real variables, the former are automatically converted to the second type.
Next, we will talk about how you can implement casting for numeric data.
Trunc
Built-in mathematical function. Trunc in "Pascal" discards the entire fractional part of the argument, leading it to the integer-type view. For example, by calling a function Trunc with argument (1.73) in the end you can get the result 1.
Syntax function: Trunc (x: real): Longint.
Round
Built-in mathematical function. The Round function rounds the argument according to the rules of mathematics to the nearest integer. For example, calling Round (1.73) will end up with 2, and Round from argument (1.11) will give 1.
Syntaxfunction: Round (x: real): Longint.
It is worth noting that there are limitations to the result of the Round and Trunc functions in Pascal. Execution will fail if this result goes beyond the values of the type Longint.
Obviously, the syntax of both built-in functionsis quite simple and allows you to use Round and Trunc in Pascal for explicit type conversion without unnecessary problems and does not cause compilation errors about type violation.</ span </ p>