Format Flags

In this chapter you will learn:

  1. What are format flags
  2. How to justify Output
  3. How to append a + sign before positive numeric values
  4. How to use space flag to line up output values
  5. How to show negative numeric inside parentheses
  6. How to pad with zeros rather than spaces
  7. How to add grouping specifiers for large value numbers
  8. How to give output an alternate conversion format

What are format flags

Flags that controls various aspects of a conversion. All format flags are single characters and follow the % in a format specification.

The flags are shown here:

FlagEffect
-Left justification
#Alternate conversion format
0Output is padded with zeros rather than spaces
spacePositive numeric output is preceded by a space
+Positive numeric output is preceded by a + sign
,Numeric values include grouping separators
(Negative numeric values are enclosed within parentheses

Justifying Output

By default, all output is right-justified. You can force output to be left-justified by placing a minus sign directly after the %.

For instance, %-10.2f left-justifies a floating-point number with two decimal places in a 10-character field.

import java.util.Formatter;
//  j  av a 2s.c  om
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    // Right justify by default
    fmt.format("|%10.2f|", 123.123);
    System.out.println(fmt);
    // Now, left justify.
    fmt = new Formatter();
    fmt.format("|%-10.2f|", 123.123);
    System.out.println(fmt);
  }
}

The output:

The following code shows how to use justify flags to align positive and negative values.

import java.util.Formatter;
//j  a  v  a2  s.  co  m
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("% d", -100); 
    System.out.println(fmt); 
 
    fmt = new Formatter(); 
    fmt.format("% d", 100); 
    System.out.println(fmt); 
 
    fmt = new Formatter(); 
    fmt.format("% d", -200); 
    System.out.println(fmt); 
 
    fmt = new Formatter(); 
    fmt.format("% d", 200); 
    System.out.println(fmt); 
  }
}

The output:

Appends a + sign before positive numeric values

Adding the + flag appends a + sign before positive numeric values.

For example,

import java.util.Formatter;
//from   j  ava2 s. c  om
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    // Right justify by default
    fmt.format("%+d", 100); 
    System.out.println(fmt);
  }
}

The output:

Line up output values

Adding the space flag outputs a space before positive values and line up the positive and negative numbers.

import java.util.Formatter;
//from j  ava  2s . co m
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("% d", -100);
    System.out.println(fmt);
    fmt = new Formatter();
    fmt.format("% d", 100);
    System.out.println(fmt);
    fmt = new Formatter();
    fmt.format("% d", -200);
    System.out.println(fmt);
    fmt = new Formatter();
    fmt.format("% d", 200);
    System.out.println(fmt);
  }
}

The output:

( flag shows negative numeric inside parentheses

The ( flag shows negative numeric inside parentheses, rather than with a leading -.

import java.util.Formatter;
/*from j  av  a 2 s. c om*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%(d", -100);
    System.out.println(fmt);
  }
}

The output:

Pad with zeros rather than spaces

The 0 flag causes output to be padded with zeros rather than spaces.

import java.util.Formatter;
//from  ja  v  a 2 s .co  m
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("0%g\n", 123.1);

  }
}

The output:

Add grouping specifiers

The comma (,) flag adds grouping specifiers.

import java.util.Formatter;
/*ja  v a  2 s.co  m*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%,.2f", 1234567890.34); 
    System.out.println(fmt);
  }
}

The output:

Alternate conversion format

The # can be applied to %o, %x, %e, and %f.

For %e and %f, the # ensures that there will be a decimal point even if there are no decimal digits.

import java.util.Formatter;
/*from j ava  2 s . c  om*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%#f", 1.0); 
    System.out.println(fmt);
  }
}

The output:

If you precede the %x format specifier with a #, the hexadecimal number will be printed with a 0x prefix.

import java.util.Formatter;
//from   ja v  a  2s  . c o  m
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%#X", 1); 
    System.out.println(fmt);
  }
}

The output:

Preceding the %o specifier with # causes the number to be printed with a leading zero.

import java.util.Formatter;
/*from j  a  v  a 2  s. com*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("%#o", 1); 
    System.out.println(fmt);
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How and when to use Uppercase Option for Java Formatter
  2. How to uppercase hexadecimal
  3. How to uppercase e symbol
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words