DMDX Help.


sprintf Keyword

<sprintf designator1[,designator2[,designator3[,designator4[,designator5[,designator6]]]]]>

counter designators:
counter.name.
c.name.
counterN
cN
N

    When used the text of a frame is treated a C printf format string (see below) and up to six counters are displayed (see <SetCounter>).  Supersedes <AppendCounter> and only one of <sprintf> or <AppendCounter> is allowed in a frame.  If anyone needs more than six counters per frame I can increase the limit easily.

    Note:  Prior to version 6.1.5.3 of DMDX some care needed to be taken when forming the frames that have the format strings in them, specifically if you say put a format specifier of %d in a frame and then go and edit it to become %02d for instance Word can toss a bunch of formatting commands (that add up to no formatting changes) in between the % and the d and DMDX wouldn't replace it with the value of the counter as one might expect and instead just display a d.  As of version 6.1.5.3 there's logic in there to remove RTF control words within a format string, no guarantees though, so if you do have trouble then deleting and retyping the %02d from scratch rectifies this if you come across it, opening and saving the file in Wordpad similarly strips out all the Word guff.

    DMDX only uses the integer and character data types and while you could specify the string and floating point types all you'll probably do is crash DMDX -- so don't use them.   Because you can use the character data type one could technically use <sprintf> to display double quotes (<set c1 = 34> "%cdouble quotes%c" <sprintf c1, c1>) or semi-colons (<set c1 = 59> "semi colon%c" <sprintf c1>) as the <sprintf> code comes after items have been parsed however when I needed to do so in the introduction I used Asian full width versions of them.

    <sprintf> is useful for doing things like displaying a clock in MM:SS format:

0 <set c1=millisectime/1000> <set c2=c1/60> <set c3=c1%60>
        "The time is %02d:%02d" <sprintf c2, c3>;

    The <SkipDisplay> error rate display example becomes significantly more elegant using <sprintf> and also demonstrates how to display a percent sign:

f60 <clfb>
0 "ErrorRate Feedback Example" <bu 1000>;
111 <set c1 = errorrate> "Error Rate %d%%" <sprintf c1> / <return>;

+1000 * "target";
+1000 * "target";
~2000 <call -111>;
 
    Note that while the Unicode option is active a %c format specifier is a UTF-16 character in the range U+0000 to U+D7FF and U+E000 to U+FFFF, otherwise without the Unicode path active it's an 8 bit ASCII one.  If you require code points from U+010000 to U+10FFFF you can use two %c format specifiers (so %c%c) and follow the usual arcane rules for encoding them in the range of D800 to DFFF.  Note that regardless of whether the Unicode option is on or not if you output one of the RTF characters that need escaping (that is \, { or }) they will need escaping with a \ before them.  In the following macro .u. contains our Unicode endpoint number:

~1 <set c1 = ~.u.> <bi 300, ~.u. .lt. 0x10000>;
~1 <md set .w. = ~.u. - 0x10000>;
~1 <set c2 = (~.w. .shr. 10) + 0xD800>
   <set c3 = (~.w. & 0x3ff) + 0xdc00>;

~300 <bi 350, ~.u. .gt. 0xffff>;
~1 <bi 310, (~.u. .ne. '\') .and. (~.u. .ne. '{') .and. (~.u. .ne. '}')>;
~1 <set c2 = '\'> <set c3 = ~.u.> <bu 350>;
310 "U+%.04x %c" <sprintf c1, c1> <bu 390>;
350 "U+%.04x %c%c" <sprintf c1, c2, c3>;
~390;

    The full version of that example is pulled from the <OnlyClearBehind> keyword's Unicode display example.

    The relevant sections of the format string documentation follow:


Format Strings

A format specification, which consists of optional and required fields, has the following form:

%[flags] [width] [.precision] type

Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %d). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to the output string. For example, to print a percent-sign character, use %%.

The optional fields, which appear before the type character, control other aspects of the formatting, as follows:

type

Required character that determines whether the associated argument is interpreted as a decimal, octal or hexadecimal number or a character (see Table 1).

flags

Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see Table 2). More than one flag can appear in a format specification.

width

Optional number that specifies the minimum number of characters output. (See printf Width Specification.)

precision

Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see Table 3).

 

Table 1   printf Type Field Characters

The type character is the only required format field ; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character or a number.

Character Output Format
c A character.
d Signed decimal integer.
i Signed decimal integer.
o Unsigned octal integer.
u Unsigned decimal integer.
x Unsigned hexadecimal integer, using abcdef.
X Unsigned hexadecimal integer, using ABCDEF.

Table 2   Flag Characters

The first optional field of the format specification is flags. A flag directive is a character that justifies output and prints signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag directive may appear in a format specification.

Flag Meaning Default
- Left align the result within the given field width. Right align.
+ Prefix the output value with a sign (+ or -) if the output value is of a signed type. Sign appears only for negative signed values (-).
0 If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and - appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) the 0 is ignored. No padding.
blank (' ') Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear. No blank appears.
# When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively. No blank appears.

printf Width Specification

The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values -- depending on whether the - flag (for left alignment) is specified -- until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).

The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).

If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.

Precision Specification

The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see Table 3). Unlike the width specification, the precision specification can cause a truncation of the output value. If precision is specified as 0 and the value to be converted is 0, the result is no characters output, as shown below:

/ <sprintf 1> "%.0d" /

If the precision specification is an asterisk (*), an argument from the argument list supplies the value. The precision argument must precede the value being formatted in the argument list.

The type determines the interpretation of precision and the default when precision is omitted, as shown in Table 3.

Table 3   How Precision Values Affect Type

Type Meaning Default
c, C The precision has no effect. Character is printed.
d, i, u, o, x, X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. Default precision is 1.

 




DMDX Index.