我用Float類來處理小數點問題.我要得到最後數值是有固定位數的小數點.有多就四捨五入.有少就不用處理,不知道在JAVA中如何處理呢?
比如:5.02356
這個數我只要三位,如何處理呢?

解决方案 »

  1.   

    import java.text.DecimalFormat;
    import java.text.NumberFormat;public class Test{
        public static void main(String[] args) {
            Float f=new Float(5.02356);
            float a=(float)0.0;
            NumberFormat form;
            form = (DecimalFormat)NumberFormat.getInstance();
            form.setMaximumFractionDigits(4);//设置小数点的位数
            try{
                a=form.parse(form.format(f)).floatValue();
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
            System.out.println("After format f is: "+a);
        }
    }
      

  2.   

    刚才的有点绕圈了,下面的更直观,简洁!import java.text.DecimalFormat;
    import java.text.NumberFormat;public class Test{
        public static void main(String[] args) {
            Float f=new Float(5.02356);
            float a=(float)0.0;
            NumberFormat form;
            form = (DecimalFormat)NumberFormat.getInstance();
            form.setMaximumFractionDigits(3);//设置小数点的位数
            f=Float.valueOf(form.format(f));
            System.out.println("After format f is: "+f);
        }
    }
      

  3.   

    楼主,我写了一个,但比较麻烦,希望抛砖引玉!
    public class InterceptDecimal {
    //sourceFloat 源Float型,index保留的小数位数
    public static float getFloat(Float sourceFloat,int index){
    char dot = '.';
    String sourceString="";
    int dotIndex;//小数点位置
    sourceString =sourceFloat.toString();        
    dotIndex = sourceString.indexOf(dot);

    //整数和小数部分
    String sourceNum,sourceDot;
    sourceNum = sourceString.substring(0,dotIndex);
    sourceDot = sourceString.substring(dotIndex+1);

    boolean blog = false;//进位标志
    //#
    //System.out.println("小数后一位"+Float.parseFloat(sourceDot.substring(index,index+1)));
    //取出要取小数后第一位判断是否大于4
    if(Float.parseFloat(sourceDot.substring(index,index+1))>4){
    blog = true;
    }
    if(blog){
    //取小数
    sourceString = sourceNum+sourceDot.substring(0,index);
    //加一
    sourceString = Float.toString(Float.parseFloat(sourceString)+1);
    //加小数点
    sourceString = sourceString.substring(0,dotIndex)+"."+sourceString.substring(dotIndex);
    //可能产生多余的.
    sourceString = sourceString.substring(0,dotIndex+index+1);

    }else{
                //取小数
    sourceString = sourceNum+sourceDot.substring(0,index);
    //加小数点
    sourceString = sourceString.substring(0,dotIndex)+"."+sourceString.substring(dotIndex);
    }
    return Float.parseFloat(sourceString);
    }
    public static void main(String[] args){
    Float s = new Float(2332.36567);
    int index = 2;
    System.out.println(InterceptDecimal.getFloat(s,index));
    }
    }
      

  4.   

    chylwk(沧海一浪) 厉害!
      

  5.   

    没有必要使用格式化吧,使用BigDecimal好过啦BigDecimal bd = new BigDecimal(2332.36567);
    bd = bd.setScale(3 , BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();后面那个我记得应该是指四舍五入的,可以看看文档说明
      

  6.   

    public static double  formatDec(float f, int n) {   //f为传进的浮点数,n为楼主希望的小数位数
            double f1;
            if (!java.lang.Float.isNaN(f)) {
                f1 = f;
            }
            else {
                f1 = 0;
            }        try {
                String decPatter = "0"; ;
                if (n > 0) {
                    decPatter = "0.";
                    for (int i = 0; i < n; i++) {
                        decPatter += "0";
                    }
                }
                NumberFormat nf = new DecimalFormat(decPatter);
                String s = nf.format(f);
                f1 = Float.parseFloat(s);
            }
            catch (Exception ex) {
                f1 = f;
            }
            return f1;
        }
      

  7.   

    public static float  formatDec(float f, int n) {   //f为传进的浮点数,n为楼主希望的小数位数
            float f1;
            if (!java.lang.Float.isNaN(f)) {
                f1 = f;
            }
            else {
                f1 = 0;
            }        try {
                String decPatter = "0"; ;
                if (n > 0) {
                    decPatter = "0.";
                    for (int i = 0; i < n; i++) {
                        decPatter += "0";
                    }
                }
                NumberFormat nf = new DecimalFormat(decPatter);
                String s = nf.format(f);
                f1 = Float.parseFloat(s);
            }
            catch (Exception ex) {
                f1 = f;
            }
            return f1;
        }
      

  8.   

    呵呵,上面是我以前做的截取double小数点位数的,刚才拿来就用,居然还带有double的痕迹