How to restrict formatting from extension

Formatter f = new Formatter(new StringBuffer());
f.format("%06d",11434235);
System.out.println(f);

displays the value 11434235

is there any way to restrict Formatterfrom expanding output?

that is, the output should be 114342instead 11434235when the format string"%06d"

+4
source share
2 answers

You can simply format it as String, so I think the best solution using format()would be

Formatter f = new Formatter(new StringBuffer());
f.format("%.6s",11434235);
+3
source

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.

+2
source

All Articles