Always use BigDecimal to check the fractional part of a number to avoid floating point errors :
require 'bigdecimal' BigDecimal.new(number).frac == BigDecimal("0.5")
For example:
BigDecimal.new("0.5").frac == BigDecimal("0.5") # => true BigDecimal.new("1.0").frac == BigDecimal("0.5") # => false
And a more general solution to see if an integer is:
BigDecimal.new("1.000000000000000000000000000000000000000001").frac.zero?
Stefan
source share