Ruby Float coerce() method with example

coerce() is a float class method which return the ceil value of the passed float value.

Syntax: float.coerce()

Parameter: numeric values (can be integer as well as float value)



Return: An array with both float and numeric value represented in the form of Float objects.

Example #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for coerce() method
  
# ceil value of the float value
puts "coerce example 1 : #{2.5.coerce(1.1)}\n\n"
  
# ceil value of the float value
puts "coerce example 2 : #{2.11115.coerce(22221)}\n\n"

chevron_right


Output :

coerce example 1 : [1.1, 2.5]

coerce example 2 : [22221.0, 2.11115]

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for coerce() method
  
# ceil value of the float value
puts "coerce example 1 : #{2.00.coerce(1.1)}\n\n"
  
  
# ceil value of the float value
puts "coerce example 2 : #{-333333.coerce(2.21)}\n\n"

chevron_right


Output :

coerce example 1 : [1.1, 2.0]

coerce example 2 : [2.21, -333333.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.