Ruby Integer ceil函数与示例
Ruby 中的ceil函数返回大于或等于 int 的最小数字,精度为ndigits十进制数字。默认值被认为是 0。当给定的精度为负时,返回值是一个整数,其中至少有ndigits.abs尾随零。当ndigits 为正时,它返回 self。
Syntax: (number).ceil(ndigits)
Parameter: The function takes the integer whose ceil value is to be returned and a single parameter ndigits which specifies the precision to be returned.
Return Value: The function returns the smallest integer number greater than or equal to it.
示例 #1:
# Ruby program of ceil function
# Initializing the numbers
num1 = 10
num2 = 17
num3 = 17.5
num4 = 21.5
# Prints the ceil value
puts num1.ceil
puts num2.ceil(1)
puts num3.ceil(0)
puts num4.ceil(2)
输出:
10
17.0
18
21.5
示例 #2:
# Ruby program of ceil function
# Initializing the numbers
num1 = 13
num2 = -90
num3 = 90
num4 = 81.7
# Prints the ceil value
puts num1.ceil
puts num2.ceil(2)
puts num3.ceil(-1)
puts num4.ceil(0)
输出:
13
-90.0
90
82