Java中的空间格式说明符
对于格式说明符,请导入以下包-
import java.util.Formatter;
创建一个格式化程序对象并设置空间格式说明符-
Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("%10d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f);
以下是显示不同格式的空间格式说明符的示例-
示例
import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("%10d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f); f = new Formatter(); f.format("%12.2f", -7.598); System.out.println(f); f = new Formatter(); f.format("% 12.3f", 90.87787); System.out.println(f); } }
输出结果
-50 90 -50 90 -7.60 90.878