Java输出格式 [英] Java output formatting

查看:203
本文介绍了Java输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Java是否有某种类来帮助输出格式化。我知道在C ++中,在iomanip中,有一个方法调用setw。我想知道是否Java有类似的东西。

解决方案

看看 java.util.Formatter



String.format()提供了一个方便的包装。 ):

  String s = String.format(e =%+ 10.4f,Math.E); 

它超越了C的printf格式。例如,它支持一个可选的语言环境,格式符号可以通过显式索引而不是隐式与一个参数相关联。

编辑:固定上面的链接。


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.

解决方案

Have a look at java.util.Formatter.

String.format() provides a convenient wrapper.

For example (modified from an example on the link):

   String s = String.format("e = %+10.4f", Math.E);

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.

Edit: Fixed the link above.

这篇关于Java输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆