Minimum Field Width

In this chapter you will learn:

  1. How to set the minimum field width for Java Formatter
  2. How to output formatter data for in a table

Specifying a Minimum Field Width

An integer between the % sign and the format conversion code acts as a minimum field-width specifier.

The default padding is done with spaces. If you want to pad with 0's, place a 0 before the field-width specifier. For example, %05d will pad a number of less than five digits with 0's so that its total length is five.

The field-width specifier can be used with all format specifiers except %n.

The following program demonstrates the minimum field-width specifier by applying it to the %f conversion:

The following code displays numbers in right justify format.
import java.util.Formatter;
//from  j  av a  2 s  .  co  m
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    // Right justify by default 
    fmt.format("|%10.2f|", 123.123); 

The output:

Produce formatted table

The minimum field-width modifier is often used to produce tables in which the columns line up.

import java.util.Formatter;
/*from   j a  v  a  2  s  .  co m*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt;
    for (int i = 1; i <= 10; i++) {
      fmt = new Formatter();
      fmt.format("%4d %4d %4d", i, i * i, i * i * i);

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to use precision modifier
  2. How to format decimal value in precision
  3. How to control the number of significant digits for %g
  4. How to control string length with precision specifier
Home » Java Tutorial » 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 class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String


    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