|
|||||
action | format, % | ||||
Related Concepts |
Syntax
string-expression (format | % ) (numeric-expression | string-expression)
An operator that converts the contents of a string-expression or numeric-expression to a format specified by the preceding string-expression. This operator allows you to dynamically format values in an OmniMark program.
For example, the output action in the following code:
match "%%" ul [digit | "abcdef"] {2} => twoHex output "b" % (twoHex base 16)
Outputs the value of the numeric expression, defined as "twoHex base 16", as a binary string.
The format operator works in a manner similar to OmniMark format items (keywords preceded by "%"), but there are two main differences. The first difference is that format items require the program to declare a variable for storing the value of the string or numeric expression before the expression can be converted, while the format operator allows you to dynamically format the value. For example, the following code creates the variable "sum" to store the numeric expression ApennySaved + ApennyEarned, then uses the %d
format item to output the sum of ApennySaved + ApennyEarned in decimal format:
set sum to ApennySaved + ApennyEarned output "%d(sum)"
The following code uses the format operator to produce the same result:
output "d" % (ApennySaved + ApennyEarned)
The second difference is that the format string can be used to allow dynamic sizing of output values. For example:
output ("d" % column-width || "d") % NumPennies
In this case, the bracketed expression is the format string used to format NumPennies.
The "format" string-expression (the expression used with the format operator to convert another string or numeric expression) must end with one of the following:
Each of these has an associated format item of the same letter. Note that "a", "b", "d", "i" and "y" require a numeric expression, just like their format item forms; and "g" requires a string expression.
Related Concepts Format items and the format operator |
---- |