Ruby 整数 to_f函数与示例
Ruby 中的to_f函数将数字的值转换为float 。如果它不适合浮点数,则返回无穷大。
Syntax: number.to_f
Parameter: The function takes the integer which is to be converted to float.
Return Value: The function returns the float value of the number.
示例 #1:
# Ruby program for to_f function
# Initializing the number
num1 = 5
num2 = 10
num3 = 1678
num4 = 1897.90
# Prints the int as float
puts num1.to_f
puts num2.to_f
puts num3.to_f
puts num4.to_f
输出:
5.0
10.0
1678.0
1897.9
示例 #2:
# Ruby program for to_f function
# Initializing the number
num1 = 51
num2 = 10.78
num3 = 1690
num4 = 183
# Prints the int as float
puts num1.to_f
puts num2.to_f
puts num3.to_f
puts num4.to_f
输出:
51.0
10.78
1690.0
183.0