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 ==)
qichunren
  • 4,405
  • 7
  • 37
  • 48

3 Answers3

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)

A little more on be_close here...

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
0

Take a look at this post: What is the most effective way for float and double comparison?

Community
  • 1
  • 1
Shuo
  • 4,749
  • 9
  • 45
  • 63