2010-12-06 10 views
0
describe "common methods" do 

    it "should get right settlement percent" do 
     contract = Contract.new 
     contract.settlement_percent = 1.1/100.0 
     contract.settlement_percent.to_f.should == 0.011 
     contract.settlement_percent.to_s.should == "0.011" 
    end 

    end 

1) Contract common methods should 
Failure/Error: contract.settlement_percent.to_f.should == 0.011 
expected: 0.011, 
     got: 0.011000000000000001 (using ==) 

答えて

0

Base-10の浮動小数点数は、バイナリでエンコードされているため、常に近似されます。値が正確であると期待するべきではありません。

2

あなたはこの近似の問題を考慮してbe_closeメソッドを使用することができます。値を渡して、比較をどれだけ近づけたいかを伝えてください。

このような何かがあなたのために働く必要があります。

contract.settlement_percent.to_f.should be_close(0.011, 0.0001) 

A little more on be_close here...

関連する問題