Ruby Float to_r() method with example
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 :
a = 2 . 0
b = 9 . 99991
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 :
a = 0 . 767
b = 2797999 . 011
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
Last Updated :
07 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...