Java-Format Flags

Format Flags

Formatter recognizes a set of format flags that lets you control various aspects of a conversion. All format flags are single characters, and a format flag follows the % in a format specification. Some of those:

Format Flags

Left justification

By default, all output is right-justified. That is, if the field width is larger than the data printed, the data will be placed on the right edge of the field. You can force output to be left-justified by placing a minus sign directly after the %. For instance, %–8d left-justifies a decimal number in a 8-character field. For example:

Format Flags

The second line is left-justified within a 8-character field.

The +, 0, ( and Comma Flags

To cause a + sign to be shown before positive numeric values, add the + flag. To show negative numeric output inside parentheses, rather than with a leading –, use the ( flag. The 0 flag causes output to be padded with zeros rather than spaces. When displaying large numbers, it is often useful to add grouping separators, which in English are commas. For example, the value 1234567 is more easily read when formatted as 1,234,567. To add grouping specifiers, use the comma (,) flag. For example:

Format Flags


Format Flags


Program Source

import java.util.Formatter;

public class Javaapp {

    public static void main(String[] args) {

        Formatter fmt = new Formatter();

Leave a Comment