arc_lupus
arc_lupus

Reputation: 4114

What does WriteLN in this case do?

I was just wondering what the following command in a pascal program does:

WRITELN(MaxTab[index,1]:7:5,'          ',
        MaxTab[index,2]:8:3,'          ',
        MaxTab[index,3]:5:1);

MaxTab is defined as ARRAY[1..200,1..3] OF REAL, and index is a counter. Usually WRITELN simply prints the text which is written in the brackets or the variables, but I do not understand what the numbers behind ] are for (e.g. ]:7:5).

Upvotes: 0

Views: 347

Answers (2)

Cyrille
Cyrille

Reputation: 25144

This is a Pascal construct similar to sprintf("%07.5f") in C-like languages. From the FreePascal documentation:

For real values, you can use the aforementioned syntax to display scientific notation in a specified field width, or you can convert to fixed decimal-point notation with:

Value : field_width : decimal_field_width

The field width is the total field width, including the decimal part. The whole number part is always displayed fully, so if you have not allocated enough space, it will be displayed anyway.

However, if the number of decimal digits exceeds the specified decimal field width, the output will be displayed rounded to the specified number of places (though the variable itself is not changed).

write (573549.56792:20:2);

would look like (with 11 spaces in front):

           573549.57

Upvotes: 3

The value after the first colon determines the width of the field in characters, the second value determines the number of digits to show following the decimal point.

Upvotes: 2

Related Questions