Format Specifiers

InstallShield 2019 » InstallScript Language Reference

Format specifiers are used with the functions Sprintf and SprintfBox to control the display of values that are stored in variables. A format specifier begins with a percent sign (%) and is followed by at least one or two characters. Format specifications follow the format shown below:

% [-] [#] [0] [width] [.precision] type

Each field of a format specification is a single character or number that represents a particular format option. The type field, for example, determines whether Sprintf or SprintfBox interprets the associated argument as a character, a string, or a number. The initial character % and the type field are both required. Items enclosed within brackets are optional. The simplest format specification contains only the percent sign and a type character, for example %s.

In the following example, the value of svString is displayed in a message box. The format specifier %s, which is assigned to svFormat, indicates to SprintfBox that the value of svString should be displayed as a string of characters.

    STRING szTitle, szFormat, szString;

    

    szTitle  = "Demonstrate format specifiers";

    szFormat = "%s";

    szString = "This is a string.";

    

    SprintfBox(INFORMATION, szTitle, szFormat, szString);

The value assigned to svFormat may contain literal characters (including escape sequences) that are to be displayed along with the value of a variable. In the following example, an identifying label is displayed to the left of a number variable: nNumber = 100;.

    STRING szTitle, szFormat;

    NUMBER nNumber;

    

    szTitle  = "Demonstrate format specifiers";

    szFormat = "nNumber = %d.";

    nNumber = 100;

    

    SprintfBox(INFORMATION, szTitle, szFormat, nNumber);

Note • To print a percent sign, you must insert two percent signs in the string assigned to svFormat. Assuming that the number to be printed is 100, the following format specification string displays “nNumber = 100%”:

      svFormat = "nNumber = %d%%."

Format Specifier Fields

Field

Meaning

-

If you include a hyphen after the percent character, the output value is aligned left and padded on the right to the width of the field with blanks or zeros. If you omit this field, the output value is right aligned and padded on the left.

#

Use this symbol to prefix hexadecimal values with 0x (lowercase) or 0X (uppercase).

0

Pads the output value with zeros to fill the field width. If you omit this field, the output value is padded with blanks.

width

Enter the minimum number of characters you want to place in this field. Type the width field as a non-negative integer. When you enter a width specification, the value is never truncated. If the number of characters in the output value is greater than the width specified, or if you omit the width field, every character of the value is displayed, subject to the value of the precision field.

precision

Enter the minimum number of digits you want in this field. If the number of digits in the argument is less than the precision value you enter, the output value on the left is padded with zeros. When the number of digits exceeds the precision value, the value is not truncated. If you enter a precision value of zero or omit it entirely, or if the period (.) appears without a number following it, the precision is set to 1. For strings, convert the maximum number of characters.

type

Format the corresponding argument as a character, a string, or a number. When two format specifier letter combinations are shown, you can use one or the other, but not both at the same time. This is a required field. In this field you must enter one of the following characters:

c—Formats a single character of type CHAR. The Sprintf function ignores a character with a numeric value of zero.
d, i—Formats a single integer of type INT or of type NUMBER.
ld, li—Formats a single signed decimal integer of type LONG.
lx, lX—Formats a single unsigned hexadecimal integer of type LONG.
s—Formats a string (type STRING).

Each format specifier has a matching variable. The variables are listed from left to right after the string, with the first variable matching the first format specifier in the string, the second variable matching the second format specifier in the string, and so on. At run time, InstallShield inserts each variable's contents into the string at the location of its matching format specifier.

See Also