In Ruby, we can check if the float value is less than zero (0) using the negative?
method.
float_value.negative?
float_value
: This is the value that we want to check.
It returns true
if the float_value
is less than 0. Otherwise, it returns false
.
# create some float valuesf1 = 2.3/0.0 # Infinityf2 = -1.0f3 = -9.2333/0.0 # -Infinityf4 = 0.0/0.0f5 = 221.2222# check if less than 0puts f1.negative?puts f2.negative?puts f3.negative?puts f4.negative?puts f5.negative?
f1
, f2
, f3
, f4
, f5
, and f6
.negative?
method on the float variables we’ve created and print the results on the console.