Flags (Units: A Unit Conversion Program and Scientific Calculator)

### 15.2 Flags ¶

The ‘`+`’ flag causes the output to have a sign (‘`+`’ or ‘`-`’). The space flag ‘`` ’ is similar to the ‘`+`’ flag, except that when the value is positive, it is prefixed with a space rather than a plus sign; this flag is ignored if the ‘`+`’ flag is also given. The ‘`+`’ or ‘`` ’ flag could be useful if conversions might include positive and negative results, and you wanted to align the decimal points in exponential notation. The ‘`#`’ flag causes the output value to contain a decimal point in all cases; by default, the output contains a decimal point only if there are digits (which can be trailing zeros) to the right of the point. With the ‘`g`’ or ‘`G`’ types, the ‘`#`’ flag also prevents the suppression of trailing zeros. The digit-grouping flag ‘`'`’ shows a thousands separator in digits to the left of the decimal point. This can be useful when displaying large numbers in fixed-point decimal; for example, with the format ‘`%f`’,

    You have: mile
    You want: microfurlong
            * 8000000.000000
            / 0.000000

the magnitude of the first result may not be immediately obvious without counting the digits to the left of the decimal point. If the thousands separator is the comma (‘`,`’), the output with the format ‘`%'f`’ might be

    You have: mile
    You want: microfurlong
            * 8,000,000.000000
            / 0.000000

making the magnitude readily apparent. Unfortunately, few compilers support the digit-grouping flag.

With the ‘`-`’ flag, the output value is left aligned within the specified field width. If a field width greater than needed to show the output value is specified, the ‘`0`’ (zero) flag causes the output value to be left padded with zeros until the specified field width is reached; for example, with the format ‘`%011.6f`’,

    You have: troypound
    You want: grain
            * 5760.000000
            / 0000.000174

The ‘`0`’ flag has no effect if the ‘`-`’ (left align) flag is given.