Java Formatter format flags

Introduction

The format flags control various aspects of a conversion in Java Formatter class.

All format flags are single characters, and a format flag follows the % in a format specification.

The flags are shown here:

Flag Effect
- Left justification
# Alternate conversion format
0 Output is padded with zeros rather than spaces
space Positive 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.

If the field width is larger than the data printed, the data is placed on the right edge of the field.

To force left-justified output, add 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.

For example, consider this program:

// Demonstrate left justification.  
import java.util.*;  
  
class LeftJustify {  
  public static void main(String args[]) {  
    Formatter fmt = new Formatter();  
  
    // Right justify by default  
    fmt.format("|%10.2f|", 123.123);  
    System.out.println(fmt);  /*from   w  ww  .j av a  2  s . co m*/
    fmt.close();  
  
    // Now, left justify.  
    fmt = new Formatter();  
    fmt.format("|%-10.2f|", 123.123);  
    System.out.println(fmt);  
    fmt.close();  
  }  
} 
// Demonstrate left justification. 
import java.util.Formatter; 
 
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); //from  ww w. j  a  va2s. c o m
    fmt.close();
 
    // Now, left justify. 
    fmt = new Formatter(); 
    fmt.format("|%-10.2f|", 123.123); 
    System.out.println(fmt); 
    fmt.close();
  } 
}

The Space, +, 0, and ( Flags

To add a + sign before positive numeric values, use + flag.

For example,

fmt.format("%+d", 100); //+100 

Demonstrate left justification.

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

To output a space before positive values so that positive and negative values line up.

Use the space format specifiers.

// Demonstrate the space format specifiers. 
import java.util.Formatter; 
 
public class Main { 
  public static void main(String args[]) { 
    Formatter fmt = new Formatter(); 
 
    fmt.format("% d", -100); 
    System.out.println(fmt); /*from w  w  w.  ja v a 2s .  co m*/
    fmt.close();
 
    fmt = new Formatter(); 
    fmt.format("% d", 100); 
    System.out.println(fmt); 
    fmt.close();
 
    fmt = new Formatter(); 
    fmt.format("% d", -200); 
    System.out.println(fmt); 
    fmt.close();
 
    fmt = new Formatter(); 
    fmt.format("% d", 200); 
    System.out.println(fmt); 
    fmt.close();
  } 
}

To show negative numeric output inside parentheses, rather than with a leading -, use the ( flag.

For example,

fmt.format("%(d", -100); 

creates this string:

(100) 

Comma Flag

To display large numbers with grouping separators, use the comma (,) flag.

fmt.format("%,.2f", 4123783123.34); 

creates this string:

4,123,783,123.34 

# Flag

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.

For %x, a # adds a 0x prefix to the hexadecimal number.

For %o, # causes the number to be printed with a leading zero.




PreviousNext

Related