Difference between revisions of "Basic Pascal Tutorial/Chapter 2/Formatting output"

From Lazarus wiki
Jump to navigationJump to search
m (bypass language bar/categorization template redirect [cf. discussion])
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Formatting output}}
+
{{Basic Pascal Tutorial/Chapter 2/Formatting output}}
{{TYNavigator|Output|Files}}
+
{{TYNavigator|Chapter 2/Output|Chapter 2/Files}}
  
 
2C - Formatting Output (author: Tao Yue, state: unchanged)
 
2C - Formatting Output (author: Tao Yue, state: unchanged)
  
 
Formatting output is quite easy. For each identifier or literal value on the argument list, use:
 
Formatting output is quite easy. For each identifier or literal value on the argument list, use:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
Value : field_width
 
Value : field_width
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 12: Line 13:
  
 
Suppose we had:
 
Suppose we had:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
write ('Hi':10, 5:4, 5673:2);
 
write ('Hi':10, 5:4, 5673:2);
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 20: Line 22:
  
 
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:
 
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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
Value : field_width : decimal_field_width
 
Value : field_width : decimal_field_width
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
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).
 
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).
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
write (573549.56792:20:2);
 
write (573549.56792:20:2);
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 32: Line 36:
 
             573549.57
 
             573549.57
  
{{TYNavigator|Output|Files}}
+
{{TYNavigator|Chapter 2/Output|Chapter 2/Files}}

Latest revision as of 16:18, 20 August 2022

български (bg) Deutsch (de) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

2C - Formatting Output (author: Tao Yue, state: unchanged)

Formatting output is quite easy. For each identifier or literal value on the argument list, use:

Value : field_width

The output is right-justified in a field of the specified integer width. If the width is not long enough for the data, the width specification will be ignored and the data will be displayed in its entirety (except for real values — see below).

Suppose we had:

write ('Hi':10, 5:4, 5673:2);

The output would be (that's eight spaces before the Hi and three spaces after):

        Hi   55673

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
 ◄   ▲   ►