Index

DoubleFormat

The datatype used for specifying the printing format of a Double variable.

Input Format:
A specifier string enclosed in double quotation marks.

Example 1:
The following example creates a format specifier variable that is designed to print a Double variable with a leading sign, 3 fraction digits after the decimal point, and with sufficient spaces before it to make it take up at least 5 characters in total. To show the result of using this, the example print statement that follows it, prints PI using this format specifier, surrounded by open and close parentheses.
  DoubleFormat dfmt = "+5.3f"
  print "(", $format_double($dfmt, 3.14159265), ")"
This produces the following output:

  (  +3.142)

Example 2:
The following examples show the same number being printed using a number of different format specifiers. In this case formal specifiers are are written in place as the first arguments of the $format_double() function.

Example command Output Explanation of the format
print $format_double(".3f", 54.27305555) 54.273 Print the integer part plus 3 decimal places
print $format_double(".3g", 54.27305555) 54.3 Print 3 significant figures of the number
print $format_double(".1s", 54.27305555) 54:16:23.0 Print in sexagesimal (base 60) with 1 decimal place

The syntax of the format specifiers.
The format specifier strings are a subset of the double precision formats accepted by the C language printf function, plus an extra "s" style, used for printing numbers in sexagesimal. The following shows the components of a specifier, where optional components are shown here enclosed in square brackets.

 [flags][width][.precision]style

As shown, the specification optionally starts with any combination of the following flag characters.

Flag character Meaning
"-" Left justify the printed number within the specified field width
"+" Always prefix the number with its sign.
" " (space)
If the number is positive and the "+" flag is absent, prefix the number with a space.
"#" Always display a decimal point even if no fraction digit follows it, and don't strip off trailing zeros.
"0" (zero)
If the specified field width is greater than the amount of space needed to print the number, and the "-" flag has not been specified, precede the number with sufficient spaces to make the printed number have this width.

After the optional flags, is an optional integer minimum field width. This indicates the minimum number of characters that should be used to print the number. If this is wider than the space needed to print the number, then the number is by default padded with leading spaces to make it occupy the specified width. However if the "0" flag is specified, then it is padded with leading zeroes, unless the "-" flag is also specified, which causes it to be padded with trailing spaces.

The next optional component is the precision, which is indicated by a decimal point, followed by an integer. The meaning of this number differs depending on the printing style. See the table below.

The final component of format specifier is a character that indicates the printing style. This is the only non-optional component of the specifier. It must be one of the following.

Style character Meaning
"f" Print the integer part of the number, followed by a decimal point, followed by the number of fractional digits specified by the precision. The default precision when not given is usually 6.
"e" Use power-of-ten exponential notation, like 2.3e+05, to print the number. The precision specifies the number of decimal places to display in the mantissa, and usually defaults to 6.
"g" Use either the "f" or the "e" format, depending on the size of the number to be printed. The precision indicates the number of significant figures to be printed, and usually defaults to 6.
"s" Print the number is sexagesimal (base 60) format, like +34:12:54.23. The precision indicates the number of fraction digits to displace in the final seconds field, and defaults to zero.


Martin Shepherd (3-Nov-2010)