[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Format Verbs

The general form for fomat verbs is:

	%[flags]verb

Table 3-1 lists the format verbs available for the print function.
Table 3-1 Format verbs for console output
%b prefix for big; e.g., %bd is a decimal big
%c character
%d decimal int
%e Same as %f with e prefix
%E Same as %f with E prefix
%f real as [-]digits[.digits]
%g real, choosing most natural of %e or %f
%G real, choosing most natural of %E or %f
%r most recent system error string; clears error
%s string
%x hexadecimal int, ref ADT

Flags

The 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.

Minimum Field Width

The minimum field width flag pads the output with spaces to ensure that it is at least the specified minimum length. If the string or number is longer than the minimum, it is still printed in full.

For example:

	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

Precision

The precision flag follows the minimum field width flag, if there is one. The precision is specified by a period followed by an integer. Its exact meaning depends on the data type.

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.

For example:

	decnum := 2345.678901;
	sys->print("%.3f\n", decnum);

This prints:

	2345.679

If 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.

For example:

	str := "Hello, Inferno!";
	sys->print("%5.10s\n\n", str);

This prints:

	Hello, Inf

If 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.

For example:

	num := 123;
	sys->print("%2.5d\n", num);

This prints:

		00123

Justification

By default, output is right-justified. You can force output to be left-justified using the justification flag, the minus sign (-). Placing the justification flag immediately after the %, prints the data left justified.

For example:

	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

Decimal and Hexidecimal Numbers

If you use the pound sign (#) 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.

For example:

	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



[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Copyright © 1998, Lucent Technologies, Inc. All rights reserved.