Overview

Text and number controls include a Format field that allows you to use a sprintf formatting string to manage the display of the output of these controls.

The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output string and conversion specification.

Using the conversion specification

The conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width and an optional precision.

For example: %0.2f would produce an output in the style ddd.dd where the precision is specified to 2 decimal places.

The syntax is: ConversionSpecification = “%” { Flags } [ MinimumFieldWidth ] [ Precision ] ConversionSpecifier, where curly braces { } denote repetition and square brackets [ ] optionality.

In addition to the conversion specifier you can include any alphanumeric characters before or after the specifier including spaces to additionally change the content displayed.

For example, to add currency symbols or units of measurement: £%0.2f would produce an output in the style £ddd.dd or %0.2f litres produces an output in the style ddd.dd litres

The flags

The character % is followed by zero or more of the following flags:

+ A sign (+ or -) should always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers.
0 The value should be zero padded. This works in conjunction with the minimum field width.
(a space) The value should be space padded.
Indicates alternate padding character, specified by prefixing it with a single quote ‘.
- The converted value is to be left adjusted on the field boundary (the default is right justification).
The minimum field width

You can use an optional decimal digit string (with nonzero first digit) to specify a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given).

The precision

You can define an optional precision, in the form of a period . followed by an optional decimal digit string.

This gives the number of digits to appear after the radix character for e, E, f and F conversions, the maximum number of significant digits for g and G conversions or the maximum number of characters to be printed from a string for s conversion.

The conversion specifier

The following table lists the specifier available to define what type the output should be treated as:

s The string argument is treated as and presented as a string.
d i The integer argument is converted to signed decimal notation.
b The unsigned integer argument is converted to unsigned binary.
c The unsigned integer argument is converted to an ASCII character with that number.
o The unsigned integer argument is converted to unsigned octal.
u The unsigned integer argument is converted to unsigned decimal.
x X The unsigned integer argument is converted to unsigned hexadecimal. The letters abcdef are used for x conversions; the letters ABCDEF are used for X conversions.
f The float argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.
e E The float argument is rounded and converted in the style [-]d.ddde±dd, where there is one digit before the decimal-point character and the number of digits after it is equal to the precision. If the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent.
g G The float argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -6 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.
% A literal % is written. No argument is converted. The complete conversion specification is %%.