Ruby Float to_r() 方法与示例
Float to_r()是一个浮点类方法,它返回浮点值的有理形式。
Syntax: float.to_r()
Parameter: float value as argument
Return: Rational form representation of the float value.
示例 #1:
# 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"
输出 :
Rational form of a : 2/1
Rational Form of b : 21990034643427/2199023255552
示例 #2:
# 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"
输出 :
Rational form of a : 6908521828386341/9007199254740992
Rational Form of b : 375541070202667/134217728