Formatter f = new Formatter(new StringBuffer()); f.format("%06d",11434235); System.out.println(f);
displays the value 11434235
11434235
is there any way to restrict Formatterfrom expanding output?
Formatter
that is, the output should be 114342instead 11434235when the format string"%06d"
114342
"%06d"
You can simply format it as String, so I think the best solution using format()would be
String
format()
Formatter f = new Formatter(new StringBuffer()); f.format("%.6s",11434235);
The simplest workaround:
Formatter f = new Formatter(new StringBuffer()); f.format("%06d", 11434235); System.out.println(f.toString().substring(0, 6));
See the Eel Lee solution for a format string only solution.