Formatter f = new Formatter(new StringBuffer());
f.format("%06d",11434235);
System.out.println(f);
打印值11434235
有没有办法限制Formatter
扩展输出?
也就是说,格式字符串为114342
11434235
而不是"%06d"
答案 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针对仅包含格式字符串的解决方案的解决方案。