企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
取整 ----- 一般来说,Python中的函数、运算等默认取整 取整函数有: `int(x=0)` -- 返回整数位 `math.floor(x)` -- 返回小于参数x的最大整数,即对浮点数向下取整 ~~~ int(1.8) # 1 int(-1.8) # -1 import math math.floor(1.8) # 1 math.floor(-1.8) # -2 ~~~ 四舍五入 --------- 以下情况会采用四舍五入: 1. round()函数 2. `type==f`时的字符串格式化 注意:以上均受 计算机中浮点数的不精确性影响 #### `type==f`时的字符串格式化: 按照`.precision`指定精度表达,默认精度6 ~~~ from decimal import Decimal Decimal(1.5) # Decimal('1.5') Decimal(1.55) # Decimal('1.5500000000000000444089209850062616169452667236328125') Decimal(1.555) # Decimal('1.5549999999999999378275106209912337362766265869140625') '--{:f}--{:.1f}--{:.2f}--'.format(1.5, 1.55, 1.555) # '--1.500000--1.6--1.55--' ~~~ #### round() 四舍五入 ~~~ round(...) round(number[, ndigits=0]) -> number ~~~ - 默认精度 - 0 - 受 计算机中浮点数的不精确性影响 ~~~ round(1.234567) # 1 round(1.234567, 4) # 1.2346 # python2中:五入为远离0的最近倍数 round(0.5) # 1 round(-0.5) # -1 round(0.4) # 0 round(-0.4) # 0 # python3中:五入为最近的偶数倍数 round(0.5) # 0 round(-0.5) # 0 round(0.4) # 0 round(-0.4) # 0 ~~~