JAVA 四舍五入保留两位

4.015

http://hi.baidu.com/hct5604142/blog/item/cb2469bef28fd70319d81ff1.html

 

java保留两位小数问题:

方式一:

四舍五入  
double   f   =   111231.5585;  
BigDecimal   b   =   new   BigDecimal(f);  
double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
保留两位小数
  
---------------------------------------------------------------  

方式二:

此方法保留2位小数时是真正意义上的四舍五入

java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");  
df.format(你要格式化的数字);

例:new java.text.DecimalFormat("#.00").format(3.1415926)

#.00 表示两位小数 #.0000四位小数 以此类推...

方式三:

double x=23.5455;
NumberFormat ddf1=NumberFormat.getNumberInstance() ;
ddf1.setMaximumFractionDigits(2);
String s= ddf1.format(x) ;
System.out.print(s);

你可能感兴趣的:(java,html,Blog,F#,UP)