Python的round()函数采用“四舍六入五成双”规则,即.5时向最近偶数舍入,如round(2.5)为2,round(3.5)为4;若需传统“四舍五入”(.5总进位),应使用decimal模块的ROUND_HALF_UP模式,如Decimal('2.5').quantize(Decimal('1'), rounding=ROUND_HALF_UP)结果为3,负数同理向远离零方向进位。

在Python里想做四舍五入,你首先想到的肯定就是内置的
round()
.5
decimal
Python内置的
round()
.5
# round() 的默认行为:银行家舍入
print(f"round(2.5) 的结果是: {round(2.5)}") # 2 (向偶数2靠拢)
print(f"round(3.5) 的结果是: {round(3.5)}") # 4 (向偶数4靠拢)
print(f"round(2.4) 的结果是: {round(2.4)}") # 2
print(f"round(2.6) 的结果是: {round(2.6)}") # 3
print(f"round(-2.5) 的结果是: {round(-2.5)}") # -2 (向偶数-2靠拢)
print(f"round(-3.5) 的结果是: {round(-3.5)}") # -4 (向偶数-4靠拢)
# 指定小数位数
print(f"round(2.125, 2) 的结果是: {round(2.125, 2)}") # 2.12 (向偶数2靠拢)
print(f"round(2.135, 2) 的结果是: {round(2.135, 2)}") # 2.14 (向偶数4靠拢)如果你需要的是传统意义上的“四舍五入”(round half up),即遇到
.5
decimal
from decimal import Decimal, ROUND_HALF_UP, getcontext
# 使用 decimal 模块实现传统四舍五入
# 先设置精度,虽然这里主要控制的是舍入模式
getcontext().prec = 10 # 设置默认精度,对 quantize 影响不大,但习惯性设置
# 或者直接在 quantize 时指定精度
print(f"Decimal('2.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('2.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # 3
print(f"Decimal('3.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('3.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # 4
print(f"Decimal('2.4').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('2.4').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # 2
print(f"Decimal('2.6').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('2.6').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # 3
# 处理负数时,传统四舍五入通常意味着向远离零的方向进位
print(f"Decimal('-2.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('-2.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # -3
print(f"Decimal('-3.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('-3.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # -4
# 指定小数位数进行传统四舍五入
print(f"Decimal('2.125').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('2.125').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)}") # 2.13
print(f"Decimal('2.135').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('2.135').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)}") # 2.14如果只是简单地对正数进行传统四舍五入到整数,或者你对浮点数的精度要求没那么极致,也可以自己写一个函数,利用
math.floor
math.ceil
立即学习“Python免费学习笔记(深入)”;
import math
def traditional_round(number):
"""
实现传统意义上的四舍五入(round half up)。
对于正数,0.5及以上进位;对于负数,-0.5及以下舍去(向零方向)。
注意:这与 Decimal 的 ROUND_HALF_UP 对负数的处理略有不同。
"""
if number >= 0:
return math.floor(number + 0.5)
else:
# 对于负数,-2.5 应该变成 -2,-2.6 变成 -3
# 这里的逻辑是:如果 -2.5 加上 0.5 变成 -2.0,floor 还是 -2
# 如果 -2.6 加上 0.5 变成 -2.1,floor 还是 -3
# 这种处理方式可能更符合某些场景下的“向零舍入”
return math.ceil(number - 0.5) # 或者更常见的,-2.5 变成 -3
# 传统四舍五入对负数通常是远离零进位
# 比如 -2.5 变为 -3
# 我们可以这样实现:
# return math.copysign(math.floor(abs(number) + 0.5), number) # 更通用且符合传统
# 这样 -2.5 -> -3, -2.4 -> -2
# 考虑到更常见的负数四舍五入(例如-2.5变成-3),这里我调整一下
return math.floor(number + 0.5) if number >= 0 else math.ceil(number - 0.5)
# 重新定义一个更符合传统“远离零”的四舍五入函数
def round_half_up_away_from_zero(number):
"""
实现传统意义上的四舍五入(round half up),
对于正数和负数都向远离零的方向进位。
例如:2.5 -> 3, -2.5 -> -3
"""
return math.copysign(math.floor(abs(number) + 0.5), number)
print(f"traditional_round(2.5) 的结果是: {traditional_round(2.5)}") # 3
print(f"traditional_round(3.5) 的结果是: {traditional_round(3.5)}") # 4
print(f"traditional_round(-2.5) 的结果是: {traditional_round(-2.5)}") # -2 (这里是向零舍入)
print(f"round_half_up_away_from_zero(-2.5) 的结果是: {round_half_up_away_from_zero(-2.5)}") # -3 (远离零进位)
print(f"round_half_up_away_from_zero(-2.4) 的结果是: {round_half_up_away_from_zero(-2.4)}") # -2可以看到,对负数的处理,不同的“传统四舍五入”定义会有差异。
decimal
ROUND_HALF_UP
round()
每次我跟一些刚接触Python的朋友聊到
round()
round(2.5)
round(3.5)
那么,为什么会有这种“奇葩”的规则呢?这背后其实有很深奥的数学和统计学考量。在大量的数值计算中,如果总是简单地“逢五进一”,会导致一个微小的向上偏差累积。举个例子,假设我们有一系列以
.5
.5
所以,当你看到
round(x.5)
既然Python内置的
round()
最推荐且最稳妥的方法是使用Python标准库中的
decimal
float
from decimal import Decimal, ROUND_HALF_UP
# 传统四舍五入到整数
num1 = Decimal('2.5')
num2 = Decimal('3.5')
num3 = Decimal('-2.5') # 负数也向远离零的方向进位
print(f"{num1} 传统四舍五入到整数: {num1.quantize(Decimal('1'), rounding=ROUND_HALF_UP)}") # 3
print(f"{num2} 传统四舍五入到整数: {num2.quantize(Decimal('1'), rounding=ROUND_HALF_UP)}") # 4
print(f"{num3} 传统四舍五入到整数: {num3.quantize(Decimal('1'), rounding=ROUND_HALF_UP)}") # -3这里的关键是
quantize()
ROUND_HALF_UP
quantize(Decimal('1'), ...)rounding=ROUND_HALF_UP
当然,如果你对精度要求没那么高,或者只是处理正数,也可以自己构建一个简单的函数。前面提到过,利用
math.floor
import math
def custom_round_half_up(number):
"""
实现传统四舍五入(round half up),对正负数都适用,
遵循“远离零进位”的原则。
"""
return math.copysign(math.floor(abs(number) + 0.5), number)
print(f"custom_round_half_up(2.5): {custom_round_half_up(2.5)}") # 3.0
print(f"custom_round_half_up(2.4): {custom_round_half_up(2.4)}") # 2.0
print(f"custom_round_half_up(-2.5): {custom_round_half_up(-2.5)}") # -3.0
print(f"custom_round_half_up(-2.4): {custom_round_half_up(-2.4)}") # -2.0这个
custom_round_half_up
float
decimal
2.0000000000000001
float
2.0
2.4999999999999999
2.5
所以,我的建议是:如果对精度有要求,或者需要处理复杂的金融、科学计算,毫不犹豫地选择
decimal
负数的四舍五入,其逻辑往往比正数更让人纠结,因为它涉及到“向零舍入”还是“远离零舍入”的问题。不同的标准和应用场景,对负数的舍入定义可能大相径庭。Python在这一点上,也体现了其灵活但又需要我们明确理解的特性。
我们先看看Python内置的
round()
print(f"round(-2.5) 的结果是: {round(-2.5)}") # -2
print(f"round(-3.5) 的结果是: {round(-3.5)}") # -4
print(f"round(-2.4) 的结果是: {round(-2.4)}") # -2
print(f"round(-2.6) 的结果是: {round(-2.6)}") # -3从结果可以看出,
round()
-2.5
-2
-3.5
-4
然而,当我们谈到“传统四舍五入”时,对负数的处理通常意味着“远离零进位”。也就是说,
-2.5
-3
-2
这时,
decimal
ROUND_HALF_UP
from decimal import Decimal, ROUND_HALF_UP
print(f"Decimal('-2.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('-2.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # -3
print(f"Decimal('-3.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('-3.5').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # -4
print(f"Decimal('-2.4').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('-2.4').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # -2
print(f"Decimal('-2.6').quantize(Decimal('1.'), rounding=ROUND_HALF_UP) 的结果是: {Decimal('-2.6').quantize(Decimal('1.'), rounding=ROUND_HALF_UP)}") # -3你看,
Decimal('-2.5')ROUND_HALF_UP
-3
所以,在处理负数时,首先要明确你所期望的舍入规则是什么。如果你需要的是与正数对称的“银行家舍入”,那么内置的
round()
.5
-2.5
-3
decimal
ROUND_HALF_UP
以上就是Python怎么进行四舍五入_Python数值四舍五入方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号