JAVA中精确保留到小数点后2(n)位的办法

时间:2021-12-20 18:34:37



1 java.text.DecimalFormat df = new java.text.DecimalFormat("#0.##");  
2 double d=3.14159;  
3 System.out.println(df.format(d));

1 java.math.BigDecimal bd = new java.math.BigDecimal("3.14159265");  
2 bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);

01 class Test{  
02       public static void main(String se77en[]){  
03           double ret = convert(3.14159);  
04           System.out.println(ret);  
05       }  
06          
07       static double convert(double value){  
08           long l1 = Math.round(value*100);   //四舍五入  
09           double ret = l1/100.0;    //注意:使用100.0而不是100  
10           return ret;  
11       }  
12   }

1 double d = 3.14159;  
2 d = (double)((int)(d*100+0.5))/100;



<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(2243) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议