FORMAT

Synopsis

HELP:   Defines a format string to convert numbers
TYPE:   STRING
SYNTAX: FORMAT='str'

Description

Provides formatting of strings containing decimal numbers to another string matching the specified format string. When converting a floating point number, conversion depends on the locale. For the conversion to be successful, the in-memory representation must be in the local character set. Use CCSID=DEFAULT to ensure this. If no character conversion is requested, the local character set will be assumed. Other kinds of character conversions mainly result in an error or strange data. The resulting formatted string is truncated if the output buffer is too small.

The format string specifies the format of the number in the output string. Numbers may be decimal integers or real numbers, signed or unsigned.

The conversion ignores leading whitespace and parses the number up to the first character not part of the number. If this is not the last character, an error will occur unless the 'IGNREST' switch is set (may be used e.g. to ignore currency signs).

Input number format

The input numbers must match one of the following regular expressions in order to be processed successfully. Internally, the numbers are converted to a 64 bit integer or floating point value.

Floating point (f,g,G,e,E):
   [:space:]*[+|-][:digit:]+.[:digit:]+e|E[:digit:]+
   [:space:]*[+|-][:digit:]+,[:digit:]+e|E[:digit:]+
Integer (i,d,u,x,X):
   [:space:]*[+|-][:digit:]+

Format string

A format string must start with the % character, otherwise the format string is ignored. The percent character is followed by optional flags, an optional width, an optional precision and a format specifier. Precision and width are separated by a period:

Format string:
   %[flags][width][.precision]specifier
As regular expression:
   %[-|+| |0][:digit:]*[.[:digit:]+]u|d|i|x|X|e|E|F|g|G

Specifier

The specifier is the type of the output number and tells the string converter how to parse the input. A real number is parsed and stored in a 64 bit floating point value. An integer is stored in a 64 bit integer value.

Flags

Width

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with whitespace. The value is not truncated even if the result is larger.

Precision

For integer specifiers (d, i, u, x), precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E and f specifiers, this is the number of digits to be printed after the decimal point. For g and G specifiers, this is the maximum number of significant digits to be printed. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.