Module: VARIABLES. OUTPUT FORMATS


Problem

5/6

Output field width and precision

Theory Click to read/hide

*** in-depth material: for those interested in the Pascal language ***
Additional output specifiers allow you to control the output of numbers in certain formats.

1. For integer data (integer etc.)  ;- the format is set by one number, which determines the number of positions assigned to this number.
Example:
Writeln(i:5);
    Displays the value of the i variable using 5 positions for this (even if the number occupies less than 5 positions, free positions will still be displayed).
    If the variable i:=34, then 3 empty positions will be displayed on the screen (they are shown with an underscore for clarity) and the number 34, i.e. only 5 positions.

_ _ _ 34
    If the variable i:=2345666, then this format (i:5) cannot be used (the displayed number takes more positions) and will simply be ignored and the entire value of the variable will be displayed on the screen.

2345666


2. For real data (real etc.) - the format is specified either by a single number that determines the number of positions assigned to this number in exponential form; or two numbers, the first of which indicates the total size of the field reserved for this number, the second - the number of decimal places, i.e. precision.
Example.
Writeln(p:12);    or      Writeln(p:6:2);
    If the variable p:=1234.6789, then in the first case, the screen will display

_1.2346E+004, and in the second 1234.68
Example.
Let the value of the X variable be 123.45678, then the operator
         Write(X);       ;   will output         "_1.23456780000000000E+02"
         Write(X:8:2);    will output        "_ _123.46"
         Write(X:10:5);  will output        "_123.45678"
         Write(X:10);     will output        "_1.23E+002"
         Write(X:8);       will output        "_1.2E+02"
         Write(X:1);       will output     ;    "_1.2E+002"



Let's try to use this information in practice.

Problem

This is an optional activity for those who want to learn more about the Pascal inference operator.

On the 4th line, write a statement that displays the value of the variable a in a field 10 characters wide (right justified)