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
  • 165,429
  • 45
  • 277
  • 381
Jwan622
  • 11,015
  • 21
  • 88
  • 181

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
  • 30,124
  • 14
  • 81
  • 98