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
So how can i check if the number is even?
The even? function in Ruby returns a boolean value. It returns true if the number is even, else it returns false. Syntax: number.
A floating-point number or a float represents a real number. Real numbers can be either a rational or an irrational number; numbers that contain a fractional part, such as 9.0 or -116.42 . In other words, a float in a Ruby program is a number that contains a decimal point.
How do you check if a value is an integer? The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .
function in Ruby returns a boolean value. It returns true if the number is odd, else it returns false. Syntax: number. odd?
Make it an Integer
then:
a = 4.0
a.to_i == a && a.to_i.even? #=> true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With