首页 > 技术文章 > java格式化输出方法

fjw3214 2018-09-27 21:43 原文

 

##Java中实现格式化输出的几种方式:
1、System.out.printf();
类似于c语言的printf方法。如:

int x = 55;

System.out.format(“x = %5x”, x);

输出结果为:x = 37

2、System.out.format()
int x = 55;

System.out.printf(“x = %5c”, x);

输出结果为:x = 7

3、String.format()
String提供的静态方法。

int x = 55;

System.out.println(String.format(“x = %5x”, x));

输出结果为:x = 37

4、Formatter
int x = 55;

Formatter format = new Formatter(System.out);

format.format(“x = %5x”, x);

输出结果为:x = 37


本文来自 wanna_wsl 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/wanna_wsl/article/details/62237955?utm_source=copy

 

推荐阅读