JAVA中控制double类型小数点后位数的方法

控制double类型小数点后的位数

 

假如现在想把某一个double的值限制在小数点后的4位,可以这样做:

import java.text.DecimalFormat;  
  
public   class  Main {  
     public   static   void  main(String[] args) {  
               DecimalFormat df =  new  DecimalFormat(  "0.0000 " );  
                double  d1 = 1.0;  
                double  d2 = 4.56789;  
               System. out .println(df.format(d1));   
               System. out .println(df.format(d2));   
        }  
} 

 

posted @ 2016-08-07 18:38  乱世_独自美  阅读(3196)  评论(0)    收藏  举报