The general form for fomat verbs is:
%[flags]verb
Table 3-1 lists the format verbs available for the print
function.
flags
for the format verb specify additional formatting control. The flags provide control for the minimum field width, precision, justification, and decimal and hexadecimal numbers.
num := 123; for (i := 1; i <= 5; i++) { sys->print("%8d %8d %8d %8d %8d\n", i, i*i, i*i*i, i*i*i*i, i*i*i*i*i); } sys->print("%d\n", num); sys->print("%10d\n\n", num);This prints:
1 1 1 1 1 2 4 8 16 32 3 9 27 81 243 4 16 64 256 1024 5 25 125 625 3125 123 123
If you use the precision flag with a real
(floating-point) using the %f
, %e
or %E
verbs, it specifies the number of decimal places printed. If you use the precision flag with a real using the %g
or %G
verbs, it specifies the number of significant digits.
decnum := 2345.678901; sys->print("%.3f\n", decnum);This prints:
2345.679If you use the precision flag with a
string
using the %s
verb, it specifies the maximum field length. For example, %3.6
prints a string at least three but not more than six characters in length. If the string is longer than the maximum field, the string is truncated.
str := "Hello, Inferno!"; sys->print("%5.10s\n\n", str);This prints:
Hello, InfIf you use the precision flag with an
int
using the %d
verb, it specifies the minimum number of digits to be printed. If the number of digits is less than what is specified, leading zeros are added.
num := 123; sys->print("%2.5d\n", num);This prints:
00123
-
). Placing the justification flag immediately after the %, prints the data left justified.
num := 123; decnum := 2345.678901; sys->print("Right-justified: %5d\n", num); sys->print("Left-justified: %-5d\n", num); sys->print("$%9.2f\n\n", decnum);This prints:
Right-justified: 123 Left-justified: 123 $ 2345.68
#
) with the e
, E
, f
, g
, or G
verbs prints a decimal point even if there are no decimal digits.
If you use the pound sign (#
) with the x
or X
verbs, the hexadecimal number prints with the 0x
or 0X
prefix.
num := 123; sys->print("%#d\n", num); sys->print("%#e\n", real num); sys->print("%x\n", num); sys->print("%#X\n", num);This prints:
123 123.000000 7b 0X7B