NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits( 2 );    //小數後兩位
System.out.println( "a / b = " nf.format(放要設定的浮點數) );

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


arrow
arrow

    cookiesp 發表在 痞客邦 留言(0) 人氣()