
| Contents |
|||||||||||||||
| [an error occurred while processing this directive] |
9.3 Field Widths with printfAnother useful feature of printf conversion (the % directives) lies in the area of field widths. These are values included in the % conversion directive to specify the width of a field and are especially useful in lining up columns of data. The field width value is placed between the % and the conversion character such as:
will create right justified output:printf "%7d\n", 12345; printf "%7d\n", 123;
Negative values can be used to create left justified fixed width data fields. This is of particular use when creating multi-column data output:12345 123
This will output the data in left justified columns:printf "%-10d", 12345; printf "%-10d\n", 123; printf "%-10d", 1; printf "%-10d\n", 123456;
The conversion value can also be used to control the number of decimal places displayed for floating point (%f) numbers:12345 123 1 123456
This will output right justified data with varying numbers of digits after the decimal point as defined in the conversions:printf "%15.3f\n", 53/9; printf "%15.4f\n", 53/9; printf "%15.7f\n", 53/9;
5.889 5.8889 5.8888889 |