Ruby Integer to_f function with example
Last Updated :
07 Jan, 2020
The to_f function in Ruby converts the value of the number as a float. If it does not fit in float, then it returns infinity.
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.
Example #1:
num1 = 5
num2 = 10
num3 = 1678
num4 = 1897 . 90
puts num1.to_f
puts num2.to_f
puts num3.to_f
puts num4.to_f
|
Output:
5.0
10.0
1678.0
1897.9
Example #2:
num1 = 51
num2 = 10 . 78
num3 = 1690
num4 = 183
puts num1.to_f
puts num2.to_f
puts num3.to_f
puts num4.to_f
|
Output:
51.0
10.78
1690.0
183.0
Share your thoughts in the comments
Please Login to comment...