用Java中的格式化程序格式化输出
对于Formatter,请导入以下包-
import java.util.Formatter;
现在创建一个Formatter对象-
Formatter f1 = new Formatter();
现在,我们将使用Formatter格式化输出-
f1.format("Rank and Percentage of %s = %d %f", "John", 2, 98.5);以下是一个例子-
示例
import java.util.Formatter;
public class Demo {
public static void main(String args[]) {
Formatter f1 = new Formatter();
Formatter f2 = new Formatter();
f1.format("Rank and Percentage of %s = %d %f", "John", 2, 98.5);
System.out.println(f1.toString());
f2.format("Rank and Percentage of %s %s = %d %f", "David", "Warner", 1, 80.8);
System.out.println(f2.toString());
}
}输出结果
Rank and Percentage of John = 2 98.500000 Rank and Percentage of David Warner = 1 80.800000