看代码。DecimalFormat df = new DecimalFormat("#.00");
double d = 63*0.105;
        System.out.println(d);
        System.out.println(df.format(d));
        System.out.println(String.format("%.2f", d));
这边这个d只能用double型。。63*0.015应该为6.615 但是变为了6.61499999怎么破

解决方案 »

  1.   

    public class Hello {
        public static double numberWithPrecision(double value, int precision) {
            double temp = Math.pow(10, precision);
            return ((int) Math.round(temp * value)) / temp;
        }    public static void main(String[] args) {
            System.out.println(Hello.numberWithPrecision(6.615, 2));
        }
    }
      

  2.   

    http://www.java3z.com/cwbwebhome/article/article8/855.html
      

  3.   

    给你一个最基本的数据类型转换
                      double d=63*0.105;
                    double a=(d*1000+0.5);
    int b=(int)a;
    float c=b/1000f;
    System.out.println(c);
      

  4.   

    遇到这种加减乘除,四舍五入,保留几位的问题,我都是用BigDecimal类的setScale(n, RoundingMode.HALF_UP)方法。保留小数点几位,后面一个参数是四舍五入的模式。
      

  5.   

    public static double round(double v, int scale) {
    if (scale < 0) {
    throw new IllegalArgumentException(
    "The scale must be a positive integer or zero");
    }
    BigDecimal b = new BigDecimal(Double.toString(v));
    BigDecimal one = new BigDecimal("1");
    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }