红宝石 |有理 to_f()函数
to_f()是 Ruby 中的一个内置函数,返回浮点值
Syntax: rat.to_f()
Parameters: The function accepts no parameter
Return Value: It returns the float value
示例 1 :
#Ruby program for to_f() method
#Initialize rational number
rat1 = Rational(9, -2)
#Prints the rational number
puts rat1.to_f()
输出:
-4.5
示例 2 :
#Ruby program for to_f() method
#Initialize rational number
rat1 = Rational(7)
#Prints the rational number
puts rat1.to_f()
输出:
7.0