標題:
printf() 函式 (一)
[打印本頁]
作者:
tonyh
時間:
2018-2-10 14:28
標題:
printf() 函式 (一)
本帖最後由 tonyh 於 2019-2-18 19:33 編輯
假設本練習中的四個變數分別為
double a=3.456789;
double b=34.56789;
double c=345.6789;
double d=3456.789;
試利用printf()函式完成如下之執行畫面:
public class Ch64
{
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); //四捨五入至小數點後第3位
System.out.printf("%.2f%n",b);
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);
}
}
複製代碼
作者:
洪榜蔓
時間:
2018-2-10 15:09
本帖最後由 洪榜蔓 於 2018-2-10 15:23 編輯
public class Ch54
{
public static void main(String[] args)
{
double a=3.456789;
double b34.56789;
double c=345.6789;
double d=3456.789;
System.out.printf("%.3f5n",a);
System.out.printf("%.2f5n",b);
System.out.printf("%.1f5n",c);
System.out.printf("%.0f5n",d);
System.out.printf("%9.3f5n",a);
System.out.printf("%9.3f5n",b);
System.out.printf("%9.3f5n",c);
System.out.printf("%9.3f5n",d);
}
}
複製代碼
作者:
莊旻叡
時間:
2018-2-10 15:09
本帖最後由 莊旻叡 於 2018-2-24 14:09 編輯
public class Ch64
{
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);
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
蔡幸融
時間:
2018-2-10 15:13
public class Ch54
{
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("%.3f5n",a);
System.out.printf("%.2f5n",b);
System.out.printf("%.1f5n",c);
System.out.printf("%.0f5n",d);
System.out.printf("%9.3f5n",a);
System.out.printf("%9.3f5n",b);
System.out.printf("%9.3f5n",c);
System.out.printf("%9.3f5n",d);
}
}
複製代碼
作者:
高睿辰
時間:
2018-2-10 15:24
public class Ch54
{
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); //四捨五入至小數點後第3位
System.out.printf("%.2f%n",b);
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);
}
}
複製代碼
作者:
黃宥鈞
時間:
2018-2-18 13:24
public class Ch64
{
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);
System.out.printf("%.2f%n",b);
System.out.printf("%.1f%n",c);
System.out.printf("%.0f%n",d);
System.out.printf("%9.2f%n",a);
System.out.printf("%9.2f%n",b);
System.out.printf("%9.2f%n",c);
System.out.printf("%9.2f%n",d);
}
}
複製代碼
作者:
蕭澧邦
時間:
2018-2-23 20:22
public class Ch64
{
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); //四捨五入至小數點後第3位
System.out.printf("%.2f%n",b);
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);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2