Ruby | Numeric round() function
The round() is an inbuilt method in Ruby returns a number rounded to a number nearest to the given number with a precision of the given number of digits after the decimal point. In case the number of digits is not given, the default value is taken to be zero.
Syntax: num.round(ndigits)
Parameters: The function needs a number and ndigits which specifies the number of digits to be rounded off. If ndigits is not given then, default value is taken to be zero.
Return Value: It returns the rounded value.
Example 1:
# Ruby program for round()# method in Numeric # Initialize a number num1 = -16.7834num2 = -16.78324num3 = 16.873 # Prints roundputs num1.round(1)puts num2.round()puts num3.round() |
Output:
-16.8 -17 17
Example 2:
# Ruby program for round()# method in Numeric # Initialize a number num1 = 12.32num2 = -1321.998321num3 = -12.2321 # Prints roundputs num1.round(1)puts num2.round(2)puts num3.round(3) |
Output:
12.3 -1322.0 -12.232


