Format Specification (Units: A Unit Conversion Program and Scientific Calculator)

### 15.1 Format Specification ¶

The format specification recognized with the `--output-format` option is a subset of that for `printf`. The format specification has the form `%`[flags][width][`.`precision]type; it must begin with ‘`%`’, and must end with a floating-point type specifier: ‘`g`’ or ‘`G`’ to specify the number of significant digits, ‘`e`’ or ‘`E`’ for scientific notation, and ‘`f`’ for fixed-point decimal. The ISO C99 standard added the ‘`F`’ type for fixed-point decimal and the ‘`a`’ and ‘`A`’ types for hexadecimal floating point; these types are allowed with compilers that support them. Type length modifiers (e.g., ‘`L`’ to indicate a long double) are inapplicable and are not allowed.

The default format for `units` is ‘`%.8g`’; for greater precision, you could specify `-o %.15g`. The ‘`g`’ and ‘`G`’ format types use exponential format whenever the exponent would be less than \({-4}\) , so the value 0.000013 displays as ‘`1.3e-005`’. These types also use exponential notation when the exponent is greater than or equal to the precision, so with the default format, the value \({5\times 10^7}\) displays as ‘`50000000`’ and the value \({5\times 10^8}\) displays as ‘`5e+008`’. If you prefer fixed-point display, you might specify `-o %.8f`; however, small numbers will display very few significant digits, and values less than \({5\times 10^{-8}}\) will show nothing but zeros.

The format specification may include one or more optional flags: ‘`+`’, ‘`` ’ (space), ‘`#`’, ‘`-`’, or ‘`0`’ (the digit zero). The digit-grouping flag ‘`'`’ (apostrophe) is allowed with compilers that support it. Flags are followed by an optional value for the minimum field width, and an optional precision specification that begins with a period (e.g., ‘`.6`’). The field width includes the digits, decimal point, the exponent, thousands separators (with the digit-grouping flag), and the sign if any of these are shown.