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:
Ruby
# Ruby program for coerce() method in Numeric# Initialize a numbernum1 = 1.7num2 = 1.2# Function usedarr = num1.coerce(num2)# Prints coerce() of numputs arr |
Output:
1.7 1.2
Example 2:
Ruby
# Ruby program for ceil() method in Numeric# Initialize a numbernum1 = 2.8num2 = 0.7# Function usedarr = num1.coerce(num2)# Prints coerce() of numputs arr |
Output:
0.7 2.8


