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 ==)
Asked
Active
Viewed 288 times
0

qichunren
- 4,405
- 7
- 37
- 48
3 Answers
2
You can use the be_close
method to account for this approximation issue. Just pass it the value and how close you want the comparison to be.
Something like this should work for you:
contract.settlement_percent.to_f.should be_close(0.011, 0.0001)

Peter Brown
- 50,956
- 18
- 113
- 146
0
Base-10 floating point values are always approximated since they're encoded in binary. You shouldn't expect values to be that precise.

aceofspades
- 7,568
- 1
- 35
- 48