FP Integral Target Values
To handle all possible types of keys correctly, if you are going to convert it, I would suggest something like:
h = {:a => 54.0, :b => 572.0, :c => 1045.0, :d => 'test', :e => 1.23 } p(h.merge(h) do |k, v| v = v.to_i if v.is_a?(Float) && v.to_i == v v end)
The above code converts the Float values ββto a hash that are actually integral to Integer.
But you donβt have to do this at all. Although there is a mistrust of floating point formats, it turns out that they really are integral values.
You can trust that any value that was an integer in the database will be accurately compared to integer constants (including 0) and that you won't see rounding artifacts.
You will notice the difference, of course, if you divide the float into something other than the factor.
Digitaloss
source share