0

I get this result:

8.01 - 8.0 #=> 0.009999999999999787

I want my answer to be 0.01. What can I do to fix this? How can I get this to happen? I guess this is float math. Is this due to floating point precision?

sawa
  • 160,959
  • 41
  • 265
  • 366
Jwan622
  • 9,962
  • 16
  • 68
  • 147

1 Answers1

5

You can use Ruby's arbitrary-precision decimal data type, BigDecimal:

BigDecimal.new("8.01") - BigDecimal.new("8") # => 0.1e-1
Abe Voelker
  • 27,824
  • 14
  • 78
  • 97