红宝石 |有理 ceil()函数
ceil()是 Ruby 中的内置函数,返回大于或等于rat的最小数字,精度为ndigits十进制数字。 ndigits 默认为 0。当ndigits为正数时返回有理数,否则返回整数。
Syntax: rat.ceil(ndigits)
Parameters: The function accepts a number 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 smallest number greater than or equal to rat with a precision of ndigits
示例 1 :
# Ruby program for ceil() method
# Initialize rational number
rat1 = Rational(-4, 3)
# Prints the rational number
puts rat1.ceil()
输出:
-1
示例 2 :
# Ruby program for ceil() method
# Initialize rational number
rat1 = Rational('123.456')
# Prints the rational number
puts rat1.ceil(1)
puts rat1.ceil(-1)
输出:
247/2
130