Java 输出格式
我想知道 Java 是否有某种类来帮助输出格式化.我知道在 C++ 中,在 iomanip 中有一个方法调用 setw.我想知道Java是否有类似的东西.
I was wondering if Java has some sort of class to help on output formatting. I know in C++, in iomanip, there is a method call setw. I was wondering if Java has something similar to this.
推荐答案
看看java.util.Formatter.
String.format() 提供了一个方便的包装器.
String.format() provides a convenient wrapper.
例如(根据链接上的示例修改):
For example (modified from an example on the link):
String s = String.format("e = %+10.4f", Math.E);
它超越了 C 的 ?printf 格式.例如,它支持可选的语言环境,格式符号可以通过显式索引而不是隐式与参数关联.
It goes beyond C's ?printf formats. For example, it supports an optional locale, and format symbols can be associated with an argument by explicit index rather than implicit.
修复了上面的链接.
相关文章