假設本練習中的四個變數分別為
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
試利用printf()函式完成如下之執行畫面:
- public class Ch100 {
- 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("%.1f%n",c); //四捨五入至小數點後1位
- System.out.printf("%.0f%n",d);
- System.out.printf("%9.2f%n",a);
- System.out.printf("%9.2f%n",b); //指定總長度為9個字元
- System.out.printf("%09.2f%n",c);
- System.out.printf("%09.2f%n",d); //指定總長度為9個字元,不足的部分以0補上
- }
- }
複製代碼 |