- public class ch48
- {
- public static void main(String args[])
- {
- double a=3.456789;
- double b=34.56789;
- double c=345.6789;
- double d=3456.789;
- System.out.printf("%.3f%n",a); //%.3f 四捨五入至小數點後第三位
- System.out.printf("%.2f%n",b); //%n 換行
- System.out.printf("%.1f%n",c);
- System.out.printf("%.0f%n",d);
- System.out.printf("%9.2f%n",a); //數值的總長度為9個字元
- System.out.printf("%9.2f%n",b);
- System.out.printf("%9.2f%n",c);
- System.out.printf("%9.2f%n",d);
- }
- }
複製代碼 |