% (format)

operator

Return type:
String
Returns:
A formatted string representation of the value of the specified expression.
Syntax
string-expression % expression
    


Purpose

You can use the % operator, also called the format operator, to format the value of an expression as a string. For instance, the following program uses % to output the value of a BCD shelf item as dollars and cents:

  process
     local bcd price initial { 12.95 }
  
     output "<$,NNZ.ZZ>" % price
          

The % operator is preceded by a format string which specifies how the value is to be formatted. It is followed by the expression to be formatted. The expression to be formatted can be a string expression, an integer expression, a BCD expression, or a floating point expression.

The format strings used with each type of data are different. See

You can pad data to a certain number of characters as you format it, by using a number and the f format modifier:

  ; "8fd" pads the number bar with spaces to a width of 8
  
  import "ombcd.xmd" unprefixed
  
  process
     local bcd bar initial { 3456 }
  
     output "8fd" % bar || "END"
  ; Output: "3456    END"
          

You can generate the format string dynamically. For instance, the following program dynamically determines the width to use in formatting an integer expression. Notice that the format operator is also used to create the format string dynamically.

  ; "8fd" pads the number bar with spaces to a width of 8
  
  import "ombcd.xmd" unprefixed
  
  process
     local integer foo initial { 8 }
     local integer bar initial { 3456 }
     local string  format-string
  
     set format-string to "d" % foo || "fd"
     output format-string % bar || "END%n"
  ; Output: "3456    END"
          

In the example above, 8fd is created dynamically to format the local integer none bar/. The 8f in 8fd tells OmniMark to pad whatever is being formatted with spaces to a width of eight characters. The f is one of a number of format items you can use in specific contexts.

You must use a format string appropriate to the type of expression being formatted, or an error occurs.

You can also format the values of individual shelf items (as opposed to expressions) using format items.

format is a deprecated synonym for %.