Open In App

Ruby Float to_r() method with example

Last Updated : 07 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Float to_r() is a float class method which return a rational form of the float value.
Syntax: float.to_r() Parameter: float value as argument Return: Rational form representation of the float value.
Example #1 : Ruby
# Ruby code for to_r() method

# Initializing values
a = 2.0
b = 9.99991

# Printing result
puts "Rational form of a : #{a.to_r}\n\n"
puts "Rational Form of b : #{b.to_r}\n\n"
Output :
Rational form of a : 2/1

Rational Form of b : 21990034643427/2199023255552
Example #2 : Ruby
# Ruby code for to_r() method

# Initializing value
a = 0.767
b = 2797999.011

# Printing result
puts "Rational form of a : #{a.to_r}\n\n"
puts "Rational Form of b : #{b.to_r}\n\n"
Output :
Rational form of a : 6908521828386341/9007199254740992

Rational Form of b : 375541070202667/134217728


Next Article

Similar Reads