Open In App

Ruby | Rational to_f() function

Improve
Improve
Like Article
Like
Save
Share
Report

The to_f() is an inbuilt function in Ruby returns the float value

Syntax: rat.to_f()

Parameters: The function accepts no parameter

Return Value: It returns the float value

Example 1:




#Ruby program for to_f() method
  
#Initialize rational number
rat1 = Rational(9, -2)
  
#Prints the rational number
           puts rat1.to_f()


Output:

-4.5

Example 2:




#Ruby program for to_f() method
  
#Initialize rational number
rat1 = Rational(7)
  
#Prints the rational number
           puts rat1.to_f()


Output:

7.0

Last Updated : 19 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads