System.out.format在Java中用于格式化输出。在这里,让我们说以下是我们的长篇文章。long val = 787890;
为了格式化,我们考虑了以下证明输出合理的内容。System.out.format("%d%n", val);
System.out.format("%9d%n", val);
System.out.format("%+9d%n", val);
System.out.format("%08d%n", val);
System.out.format("%,9d%n", val);
下面是显示输出差异的完整示例。
示例
import java.util.Locale;
public class Demo {
public static void main(String []args) {
long val = 787890;
System.out.format("%d%n", val);
System.out.format("%9d%n", val);
System.out.format("%+9d%n", val);
System.out.format("%08d%n", val);
System.out.format("%,9d%n", val);
}
}
这是输出。您可以轻松注意到格式的差异。
输出结果787890
787890
+787890
00787890
787,890