數值與貨幣格式化輸出:
1.使用text套件的NumberFormat類別
1匯入text套件
import java.text.*;
2建立數值或貨幣格式物件
a. 使用與社區域設定數值物件
Numberformat 數值格式化物件= NumberFormat.getInstance();
b. 指定區域設定數值物件
Numberformat數值格式化物件= NumberFormat. getInstance(指定時區設定);
c. 使用預設區域設定貨幣物件
Numberformat 貨幣格式化物件= NumberFormat.getCarrencyInstance();
d. 指定區域設定貨幣物件
Numberformat 貨幣格式化物件= NumberFormat.getCarrencyInstance(指定時區);
3設定數值或貨幣格式化物件的整數與小數所保留最大與最小位數
數值(貨幣)格式化物件.getMaxiumIntegerDigists(最大保留整數位數);
數值(貨幣)格式化物件.getMaxiumIntegerDigists(最小保留整數位數);
數值(貨幣)格式化物件.getMaxiumFractionDigist(最大保留小數位數);
數值(貨幣)格式化物件.getMaxiumFractionDigist(最小保留小數位數);
4套用格式
String 字串物件=數值格式化物件.format(數值)
5輸出結果
System.out.println(字串物件);
全站熱搜
留言列表