欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java小數(shù)位的例子

 更新時(shí)間:2013年11月11日 14:18:03   作者:  
在java中要保留數(shù)字小數(shù)位我們有常用的四種方法,分別為:四舍五入,DecimalFormat,format,String .format與struts標(biāo)簽操作實(shí)現(xiàn),下面給出例子

方式一:
四舍五入
double   f   =   111231.5585;
四舍五入 保留兩位小數(shù),可以用String的format函數(shù),
方法如下:

復(fù)制代碼 代碼如下:

System.out.println(String.format("%.2f", x1));
System.out.println(String.format("%.2f", x2));
 

DecimalFormat轉(zhuǎn)換最簡便

復(fù)制代碼 代碼如下:

public void m2() {
       DecimalFormat df = new DecimalFormat("#.00");
       System.out.println(df.format(f));
}

例:new java.text.DecimalFormat(”#.00″).format(3.1415926)
#.00 表示兩位小數(shù) #.0000四位小數(shù) 以此類推…
方式三:
復(fù)制代碼 代碼如下:

double d = 3.1415926;
String result = String .format(”%.2f”);
 

%.2f %. 表示 小數(shù)點(diǎn)前任意位數(shù)   2 表示兩位小數(shù) 格式后的結(jié)果為f 表示浮點(diǎn)型。
方式四:
此外如果使用struts標(biāo)簽做輸出的話,有個(gè)format屬性,設(shè)置為format="0.00"就是保留兩位小數(shù)
例如
復(fù)制代碼 代碼如下:

<bean:write name="entity" property="dkhAFSumPl"  format="0.00" />

JAVA中保留N位小數(shù)的方法,例子 .
復(fù)制代碼 代碼如下:

import java.text.DecimalFormat;

public class numberFarmat {
    public static void main(String[] args) {
       double sd = 23.2558896635;

       //第一種方法 10000.0這個(gè)小數(shù)點(diǎn)后只表示保留小數(shù),和位數(shù)沒關(guān)系。
       double d1 = (double) (Math.round(sd*10000)/10000.0000000000);
       double d2 = (double) (Math.round(sd*10000)/10000.0);
       System.out.println("4位小數(shù)測試:"+d1);
       System.out.println("4位小數(shù)測試:"+d2);

       //第二種方法
       DecimalFormat df2  = new DecimalFormat("###.00");
       DecimalFormat df3  = new DecimalFormat("##.000");
       System.out.println("3位小數(shù):"+df3.format(sd));
       System.out.println("2位小數(shù):"+df2.format(sd));

    }
}
 


運(yùn)行結(jié)果如下:
4位小數(shù)測試:23.2559
4位小數(shù)測試:23.2559
3位小數(shù):23.256
2位小數(shù):23.26

相關(guān)文章

最新評論