Ruby check if even number, float

前端 未结 3 1259
长发绾君心
长发绾君心 2021-02-19 18:16

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

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 18:52

    Make it an Integer then:

    a = 4.0
    a.to_i == a && a.to_i.even?  #=> true
    

提交回复
热议问题