How to find the numerator of a float value in Ruby

Overview

We can get the numerator of a float value by using the numerator method.

Syntax

float_value.numerator

Parameters

float_value: This is the float value we want to get the numerator of.

Return value

The value returned is an integer that represents the numerator of the float value, float_value.

Note: The numerator returned is machine dependent.

Code example

# create float values
f1 = 2.0
f2 = 5 / 2
f3 = 0.666
f4 = 100.77
# get numerator
puts f1.numerator
puts f2.numerator
puts f3.numerator
puts f4.numerator

Explanation

  • Lines 2-5: We create some float values.
  • Lines 8-11: We invoke the numerator method on the float values we created. We then print the results to the console.

Free Resources