有个问题。 
假设 num1=1 num2 =0.001
当 num1+num2+num2+num2+num2+num2也就是说 num1加 num2  多次后 会发生 一个小小的错误 
不是完全正确的结果  这个问题如何解决 
高手请。 。

解决方案 »

  1.   

    intValue
    public int intValue()
    Returns the value of this Float as an int (by casting to type int). Specified by:
    intValue in class Number
    Returns:
    the float value represented by this object converted to type int
      

  2.   

    1.
    int temp = 0;
    temp = num2.intValue();sum = num1 + temp;
      

  3.   

    2.
    那你要用
    java.math 
    Class BigDecimal
    java.lang.Object
      |
      +-java.lang.Number
            |
            +-java.math.BigDecimal里的方法了。add
    public BigDecimal add(BigDecimal val)
    Returns a BigDecimal whose value is (this val), and whose scale is max(this.scale(), val.scale()). Parameters:
    val - value to be added to this BigDecimal. 
    Returns:
    this val
      

  4.   

    将float 换成double就可以了
      

  5.   

    你写成num2+num2+num2+num2+num2+num1就对!或者你把int转换成float或double也好!