import java.util.Formatter;
public class Receipt {
private double total = 0;
private Formatter f = new Formatter(System.out);
public void printTitle(){
f.format("%-15s %5s %10s\n", "Item","Qty","Price");
f.format("%-15s %5s %10s\n", "----","---","-----");
}
public void print(String name,int qty,double price){
f.format("%-15.15s %5d %10.2f\n", name,qty,price);
total += price;
}
public void printTotal(){
f.format("%-15.15s %5s %10.2f\n", "Tax","",total*0.06);
f.format("%-15.15s %5s %10s\n", "","","-----");
f.format("%-15s %5s %10.2f\n", "Total","",total*1.06);
}
public static void main(String[] args) {
Receipt receipt = new Receipt();
receipt.printTitle();
receipt.print("Jack's Magic Beans", 4, 4.25);
receipt.print("Princess Peas", 3, 5.1);
receipt.print("Three Bears Porridge", 1, 14.29);
receipt.printTotal();
}
/*
%[argument_index$][flag][width][.precision]conversion
在默認的情況下,數(shù)據(jù)是右對齊,通過“-”標(biāo)志來改變對齊方向
width可以用于各種類型的數(shù)據(jù)轉(zhuǎn)換
precision不是所有類型的數(shù)據(jù)都能使用precision,用于不同數(shù)據(jù)時的意義不同。
1.應(yīng)用于String時,它表示打印String時輸出字符的最大數(shù)量。
2.應(yīng)用于浮點數(shù)時,它表示小數(shù)部分顯示出來的位數(shù)(默認是6位小數(shù)),如果小數(shù)位數(shù)過多四舍五入,太少尾部補零。
3.應(yīng)用于整數(shù)時,由于整數(shù)沒有小數(shù)部分,則會觸發(fā)異常。
*/
}
類型轉(zhuǎn)換字符
d 整數(shù)類型
e 浮點數(shù)(科學(xué)計數(shù))
c Unicode字符
x 整數(shù)(十六進制)
b Boolean值
h 散列碼(十六進制)
s String % 字符%
f 浮點數(shù)(十進制)
posted on 2011-05-24 12:47
secret_x15 閱讀(516)
評論(0) 編輯 收藏 所屬分類:
java