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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

5.0
10.0
1678.0
1897.9

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

51.0
10.78
1690.0
183.0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.