Python ceil

Python 的 ceil 函数用于返回大于或等于指定表达式或特定数字的最小整数值。math ceil 函数的语法是:

math.ceil(number);
  • 如果数字参数是正数或负数,则返回其上限值。
  • 如果不是数字,math 方法将返回 TypeError。

Python ceil 函数示例

此函数允许您找到大于或等于数值的最小整数值。在此 ceil 函数示例中,我们将查找不同数据类型的上限值并显示输出。

import math

Tup = (10.98, 20.26, 30.05, -40.95 , 50.45) # Tuple Declaration
Lis = [-10.98, 32.65, -39.59, -42.15 , 35.97] # List Declaration

print('Value of Positive Number = %.2f' %math.ceil(10))
print('Value of Negative Number = %.2f' %math.ceil(-15))

print('Value of Tuple Item = %.2f' %math.ceil(Tup[2]))
print('Value of List Item = %.2f' %math.ceil(Lis[2]))

print("The Value of 'PI' after the Ceil function is: ", math.ceil(math.pi))

print('Value of Multiple Number = %.2f' %math.ceil(10 + 20 - 40.65))

print('Value of String Number = ', math.ceil('Python'))
math CEIL Function Example
  1. 在前两个语句中,我们直接将其用于正整数和负整数,并返回一个整数。
  2. 接下来的两个语句,我们将其用于 元组列表 项。如果您观察上面的 Python 屏幕截图,数学函数 在它们上面工作正常。
  3. 在下一个语句中,我们尝试直接将其用于多个值。
  4. 最后,我们将 math 函数应用于字符串值,它返回 TypeError。