close

import java.text.NumberFormat;

public class test1
{
    public static void main(String[] args)
    {
        int a = 100, b = 3;
        float fcalc = (float) a / b ;
       
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits( 2 );    //小數後兩位
        System.out.println( "a / b = " nf.format( fcalc ) );
       
       
        int nCount = 0;
        for(int i=0; i<args.length; i )
            if ( Integer.parseInt( args[i] ) >= 70 )
                nCount ;
        System.out.println( "70以上的有" + nCount + "個參數");
    }
}

  • 2007-04-06 11:03:25 補充

    簡單說就是以NumberFormat類別產生一個實體物件 nf
    再用 nf 物件提供的setMaximumFractionDigits方法來設定MaximumFractionDigits屬性為2,也就是小數點後幾位的意思。
    最後再用 nf物件提供的format方法來格式化運算結果fcalc
    如此,輸出的結果就會處理成小數點後兩位了

相關網址:   http://tw.knowledge.yahoo.com/question/question?qid=1007040406183
arrow
arrow
    全站熱搜

    chph 發表在 痞客邦 留言(1) 人氣()