I want to check if the number is even! I tried the following:
a = 4.0
a.is_a? Integer
=> false
a.even?
=> undefined method for Float
S
Just keep in mind how numbers are converted:
(4.0).to_i # same as Integer(4.0)
=> 4
(4.5).to_i
=> 4
(4.9).to_i
=> 4
Using round
may be safer:
(4.0).round
=> 4
(4.5).round
=> 5
(4.9).round
=> 5
Then of course you can call even
as @Yu Hao wrote:
(4.5).round.even?
=> false
You can also easily observe known floats 'feature':
(4.499999999999999).round.even?
=> true
(4.4999999999999999).round.even?
=> false