Say I have the following code:
x = 0.8 y = 1.0
What's the best way of checking that y
is equivalent to an Integer
? At the moment I'm doing:
y.to_int == y
which works, but I feel like there should be a better way.
Check if float is integer: is_integer() float has is_integer() method that returns True if the value is an integer, and False otherwise.
Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.
In other words, a float in a Ruby program is a number that contains a decimal point. Ruby will consider any number written without decimals as an integer (as in 138 ) and any number written with decimals as a float (as in 138.0 ).
You mod
the value with 1, and check if the value equals 0.
if y % 1 == 0
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