Open In App

Ruby | Numeric coerce() function

Last Updated : 19 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 number 
num1 = 1.7
num2 = 1.2

# Function used
arr = num1.coerce(num2)

# Prints coerce() of num
puts arr

Output

1.7
1.2


Example 2

Ruby
# Ruby program for ceil() method in Numeric

# Initialize a number 
num1 = 2.8
num2 = 0.7

# Function used
arr = num1.coerce(num2)

# Prints coerce() of num
puts arr

Output

0.7
2.8


 


Next Article

Similar Reads