close

【說明】

java.text 提供了 DecimalFormat 類別,
能夠用 DecimalFormat 的 format 方法,
將目標數字(須為double)格式化為指定的格式。

--------------------------------------------

【使用方法】:

1. import java.text.DecimalFormat;

2. //建構子參數可依照需求修改 //#:動態顯示,0:一定顯示
DecimalFormat df = new DecimalFormat("#,###.00"); 


3. // quotient 為 double 型態的變數
String formatQuotient = df.format(quotient);

--------------------------------------------

【範例程式】

double dividend=100000.0;    //被除數
double divisor=3.0;        //除數
double quotient=0.0;         //商數
        
quotient = dividend / divisor ;
        
//#:動態顯示 / 0:一定顯示
DecimalFormat df = new DecimalFormat("#,###.00"); 
        
//將 quotient 格式化
String formatQuotient = df.format(quotient);
        
//輸出結果
System.out.println(formatQuotient);

--------------------------------------------
【執行結果】

DecimalFormat.jpg

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 蕭小牛 的頭像
    蕭小牛

    蕭小牛的部落格

    蕭小牛 發表在 痞客邦 留言(0) 人氣()