String.format(“%.2f”, (double) 100.9) 解析:
运行示例,
public class TestFormat2 {
public static void main(String[] args) {
System.out.println(String.format("%.2f", (double) 100));
System.out.println(String.format("%.2f", (double) 100.9));
System.out.println(String.format("%.3f", (double) 200));
System.out.println(String.format("%.3f", (double) 200.01));
System.out.println(String.format("%.3f", (double) 200.1234));
System.out.println(String.format("%.3f", (double) 200.1236));
}
}
结果,
100.00
100.90
200.000
200.010
200.123
200.124