Ruby Integer to_f function with example
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:
# 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 |
Output:
5.0 10.0 1678.0 1897.9
Example #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 |
Output:
51.0 10.78 1690.0 183.0
Contact Us