红宝石 |数值 ceil()函数
ceil()是 Ruby 中的一个内置方法,通过保持小数部分的 n 位精度,返回大于或等于给定数字的最小数字。
Syntax: num.ceil(n digits)
Parameters: The function needs a number and n digits to which the precision of decimal digits is kept. In case no n digits 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.
示例 1 :
Ruby
# Ruby program for ceil() method in Numeric
# Initialize a number
num1 = -19
num2 = -18.97
num3 = 18.98
# Prints ceil() of num
puts num1.ceil()
puts num2.ceil()
puts num3.ceil()
Ruby
# Ruby program for ceil() method in Numeric
# Initialize a number
num1 = -19.897
num2 = -18.321
num3 = 190.23213
# Prints ceil() of num
puts num1.ceil(1)
puts num2.ceil(2)
puts num3.ceil(3)
输出:
-19
-18
19
示例 2 :
红宝石
# Ruby program for ceil() method in Numeric
# Initialize a number
num1 = -19.897
num2 = -18.321
num3 = 190.23213
# Prints ceil() of num
puts num1.ceil(1)
puts num2.ceil(2)
puts num3.ceil(3)
输出:
-19.8
-18.32
190.233