四種獲取小數點後兩位方法
//第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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.