Java Data Type How to - Set precision modifier, display at most 15 characters in a string








Question

We would like to know how to set precision modifier, display at most 15 characters in a string.

Answer

import java.util.Formatter;
//w w w  .  j ava2 s  .  c  o  m
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();

    fmt.format("%.15s", "Formatting with Java is now easy.");
    System.out.println(fmt);
  }
}

The code above generates the following result.