Java Format Zero Padding

In this chapter you will learn:

  1. What is zero padding
  2. Syntax for Java Format Zero Padding
  3. How to pad with zeros rather than spaces


0 padds output with zeros rather than spaces.


fmt.format("0%g\n", 123.1);


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

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


The output:

Next chapter...

What you will learn in the next chapter:

  1. What is grouping separators
  2. Syntax for grouping separators
  3. How to add grouping specifiers for large value numbers
Home »
  Java Tutorial »
    Java Langauge »
      Java Data Format
Java Formatter Class
Java Format Specifier
Java Format Specifier Uppercase
Java Format Precision
Java Format Flags
Java Format Justifying Output
Java Format Negative and Positive
Java Format Line up Space
Java Format Parentheses
Java Format Zero Padding
Java Format Comma
Java Format Alternate Conversion
Java Format Argument Index
Java Format date time
Java Format Date