Ruby | Numeric coerce() function

The coerce() is an inbuilt method in Ruby returns an array containing two numbers containing two float numbers.

Syntax: num1.coerce(num2)

Parameters: The function needs a number and ndigits to which the precision of decimal digits is kept. In case no ndigits is passed it takes 0 to be the default value.

Return Value: It returns the smallest number which is greater than or equal to the given number by keeping a precision of n digits of the decimal part.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for coerce() method in Matrix
  
# Initialize a number 
num1 = 1.7
num2 = 1.2
  
# Function used
arr = num1.coerce(num2)
  
# Prints coerce() of num
puts arr

chevron_right


Output:

1.7
1.2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for ceil() method in Matrix
  
# Initialize a number 
num1 = 2.8
num2 = 0.7
  
# Function used
arr = num1.coerce(num2)
  
# Prints coerce() of num
puts arr

chevron_right


Output:

0.7
2.8
My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.