The ceil()
function returns the smallest integer that is greater than or equal to the number whose ceiling needs to be calculated.
Figure 1 shows the mathematical representation of the ceil()
function and the corresponding expression in Ruby.
num.ceil(digits)
# where num is a number to round up
#where digits is the number of decimal places to which the precision is to be kept
This function requires n
digits to which the precision of decimal digits is kept. This is optional, and if its value is omitted, then its default value is 0.
ceil()
returns an integer that represents the ceiling of the value to which the function is applied.
The example shows how we can use the ceil()
function in Ruby.
#The following code rounds up 17.321 to 2 decimal places
(17.321).ceil(2) => 17.33
#In the case if '-n' is sent as digits to the function then, it rounds up the number to n number of digits to the left of decimal places
(18).ceil(-1) => 10
#positive integernum1=10print "num1.ceil() : ", num1.ceil(), "\n"#negative integernum2=-10print "num2.ceil() : ", num2.ceil(), "\n"#positive float valuenum3=2.1print "num3.ceil() : ", num3.ceil(), "\n"#negative float valuenum4=-2.1print "num4.ceil() : ", num4.ceil(), "\n"