What is float.zero in Ruby?

Overview

The method zero? can be invoked on a float value and returns a Boolean value. It checks if the float value is 0.0 or not. If true, it returns true. Otherwise, false is returned.

Syntax

float_value.zero?

Parameters

float_value: This is the float value we want to check to see if its value is 0.0.

Return value

This method returns a Boolean value true if float_value has the value 0.0. Otherwise, false is returned.

Example

# create float values
float_value_one = 2.3545
float_value_two = 0.0
float_value_three = -0.2
float_value_four = 8.9 / 8.9 * 0.0
# check if 0.0
puts float_value_one.zero?
puts float_value_two.zero?
puts float_value_three.zero?
puts float_value_four.zero?

Explanation

  • Lines 2 to 5: We create some float values.
  • Line 8 to 11: We use the zero method to check if the float values have the value 0.0.