代码如下:
double d1 = 27600.0;
double d2 = 40600.02;
double d3 = 0;
d3 = d1 + d2;
System.out.println(d3); 
    
float f1 = 27600.0f;
float f2 = 40600.02f;
float f3 = 0.0f;
f3= (float) f1 + f2;
System.out.println(f3);可是不管是double型还是float型后面都多了讨厌的小数点,计算机的输出如下:
68200.01999999999
68200.016请问如何才能正确输出呢?

解决方案 »

  1.   

    改成:也就是把小数点对齐就可以了
                      double d1 = 27600.04;
    double d2 = 40600.02;
    double d3 = 0;
    d3 = d1 + d2;
    System.out.println(d3); 
        
    float f1 = 27600.03f;
    float f2 = 40600.02f;
    float f3 = 0.0f;
    f3= (float) f1 + f2;
    System.out.println(f3);
      

  2.   

    我顶!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!中国IT人才网(    http://www.itbbs.net.cn    )!
    免费招聘、求职。IT人工作的天堂。希望能够为大家所用。
      

  3.   

    用BigDecimal可以得到精确的结果,
    或是,小数点的位数要一样,也可以得到正确的结果。
      

  4.   

    我试了试用BigDecimal也有很多位小数啊。
      

  5.   

    BigDecimal还有一些方法可以限制小数位的