四种获取小数点后两位方法

来源:互联网 发布:光伏电站数据采集器 编辑:程序博客网 时间:2024/06/05 23:52
//第1种方法.  可以使用  0  或者   # 当作模板
DecimalFormat  deci=new DecimalFormat("00.00");
//String 12.35
String result=deci.format(12.3456);
System.out.println(result);
 
//第2种方法. 12.34
BigDecimal  bd=new BigDecimal(12.3455);
bd=bd.setScale(3, BigDecimal.ROUND_HALF_UP);
System.out.println(bd.toString());
 
//第3种方法. 
double d=12.3456;
double res=((int)(d*100))/100.0;
System.out.println(res);
 
//第4种方法. 
double d2=12.3456;
double res2=Math.round(d2*100)/100.0;
System.out.println(res2);
0 0