如何限制Formatter扩展

时间:2013-10-01 08:36:55

标签: java formatter

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

打印值11434235

有没有办法限制Formatter扩展输出?

也就是说,格式字符串为114342

时,输出应为11434235而不是"%06d"

2 个答案:

答案 0 :(得分:3)

您可以将其格式化为String,因此我认为使用format()的最佳解决方案将是

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

答案 1 :(得分:2)

一个简单的解决方法:

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

请参阅Eel Lee针对仅包含格式字符串的解决方案的解决方案。