سؤال

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

prints the value 11434235

is there any way to restrict the Formatter from expanding the output?

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

هل كانت مفيدة؟

المحلول

You can simply format this as a String, so I think the best solution using format() will be

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

نصائح أخرى

A simple workaround:

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

See Eel Lee's solution for a solution with just a format string.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top