Ruby Float to_r()方法及示例
Float to_r() 是一个float类方法,它返回一个有理数形式的浮点数。
语法: float.to_r()
参数: 作为参数的浮点数值
返回: 浮动值的有理形式表示。
例子 #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