奇怪唉
0.3*3=0.899999999  ?? 不等于0.9
0.2*6=1.200000002  ?? 不等于1.2
自己编的以及从网上下载的java计算器代码运行后都出现类似问题,怎么样规范数据显示?请教!!!!!

解决方案 »

  1.   

    浮点型能表示的小数位有限float是7位,double是14位http://blog.csdn.net/nujiah001/article/details/6657410
      

  2.   


                    BigDecimal param = new BigDecimal(String.valueOf(0.3));
    BigDecimal result = new BigDecimal(3);
    System.out.println(result.multiply(param).doubleValue());的确是好蛋疼的做法~~~
      

  3.   

    java数据是用二进制表式的,会有精读损失
      

  4.   

    java用于精确计算会有误差的,sun公司说明了的
      

  5.   

    需要精确计算的时候把数字类型转换成BigDecimal运算
      

  6.   

    The elements of the types float and double are those values that can be represented using the IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point formats, respectively.The details of proper input conversion from a Unicode string representation of a floating-point number to the internal IEEE 754 binary floating-point representation are described for the methods valueOf of class Float and class Double of the package java.lang.//推荐用BigDecimal