Ruby | Numeric coerce() function
Last Updated :
19 Mar, 2024
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
num1 = 1 . 7
num2 = 1 . 2
arr = num1.coerce(num2)
puts arr
|
Output:
1.7
1.2
Example 2:
Ruby
num1 = 2 . 8
num2 = 0 . 7
arr = num1.coerce(num2)
puts arr
|
Output:
0.7
2.8
Share your thoughts in the comments
Please Login to comment...