Java-小数点控制

 

package 运算及类型转换类;  
  
import java.text.DecimalFormat;  
  
public class 控制小数点类 {  
     public static double decimalFormatD(int num, double d){  
            String format = "0.";  
            String result = "";  
            double db;  
              
            for(int i=0;i<num;i++)  
                format = format.concat("0");  
              
            DecimalFormat decimal = new DecimalFormat(format);  
            result = decimal.format(d);  
            db = Double.parseDouble(result);  
              
            return db;  
        }  
          
        public static float decimalFormatF(int num, float f){  
            String format = "0.";  
            String result = "";  
            float fl;  
              
            for(int i=0;i<num;i++)  
                format = format.concat("0");  
              
            DecimalFormat decimal = new DecimalFormat(format);  
            result = decimal.format(f);  
            fl = Float.parseFloat(result);  
              
            return fl;  
        }  
  
          
        public static String doubleToString(double f){         
            String s = "";  
            double a = 0;  
              
            while(f >= 1) {  
                  
                a = f%((double)10);  
                  
                s = String.valueOf((int)a) + s;  
                f=(f - a)/10;  
            }  
            return s;  
        }  
}  

 

posted @ 2015-05-17 22:17  243573295  阅读(597)  评论(0编辑  收藏  举报