python 四舍五入到小数点后两位

round down to 2 decimal in python

我需要四舍五入,应该是小数点后两位。
尝试了以下,

a = 28.266
print round(a, 2)

28.27

但期望值仅为 28.26。

您似乎需要 floor:

import math
math.floor(a * 100)/100.0

# 28.26

试试这个:

import math
a = 28.266
print((math.floor(a * 100)) / 100.0)

输出:

28.26

您似乎想要截断,而不是四舍五入。

一个简单的方法是结合地板划分 // 和常规划分 /:

>>> a = 28.266
>>> a // 0.01 / 100
28.26

除常规除法外,您还可以乘法(如 cmc 评论中所述):

>>> a // 0.01 * 0.01
28.26

同样,您可以创建一个函数来向下舍入到其他 more/less 小数。但是因为浮点数是不精确的数字,这会导致不准确。

def round_down(value, decimals):
    factor = 1 / (10 ** decimals)
    return (value // factor) * factor

print(round_down(28.266, 2))
# 28.26

但如前所述,它并不完全准确:

for i in range(0, 8):
    print(i, round_down(12.33333, i))
0 12.0
1 12.3
2 12.33
3 12.333
4 12.333300000000001 # weird, but almost correct
5 12.33332           # wrong
6 12.33333
7 12.33333

还有其他(更精确的)方法:

使用fraction模块的解决方案

A fraction 可以表示比 float 更精确的十进制数。然后可以使用 Psidom 提到的“乘法,然后乘法,然后除法”方法,但精度要高得多:

import fractions
import math

a = 28.266

def round_down(value, decimals):
    factor = 10 ** decimals
    f = fractions.Fraction(value)
    return fractions.Fraction(math.floor(f * factor),  factor)

print(round_down(28.266, 2))
# 1413/50  <- that's 28.26

并使用我对花车所做的测试:

for i in range(0, 8):
    print(i, round_down(12.33333, i))
0 12
1 123/10
2 1233/100
3 12333/1000
4 123333/10000
5 1233333/100000
6 1233333/100000
7 1233333/100000

但是创建 Fraction 不会神奇地修复不精确的 float,因此通常应该从字符串或“分子-分母对”而不是从浮动。

使用decimal模块的解决方案

您还可以使用 decimal 模块,它提供了多种舍入模式,包括向下舍入。

对于这个演示,我使用上下文管理器来避免全局更改小数舍入模式:

import decimal

def round_down(value, decimals):
    with decimal.localcontext() as ctx:
        d = decimal.Decimal(value)
        ctx.rounding = decimal.ROUND_DOWN
        return round(d, decimals)

print(round_down(28.266, 2))  # 28.26

四舍五入的结果更合理:

for i in range(0, 8):
    print(i, round_down(12.33333, i))
0 12
1 12.3
2 12.33
3 12.333
4 12.3333
5 12.33333
6 12.333330
7 12.3333300

Fraction 一样,应从字符串创建 Decimal 以避免中间不精确的浮点数。但与Fraction不同的是,Decimal的精度有限,所以对于有很多有效数字的值,它也会变得不精确。

然而,“向下舍入”只是可用选项之一。 available rounding modes 的列表很广泛:

Rounding modes

decimal.ROUND_CEILING Round towards Infinity.

decimal.ROUND_DOWN Round towards zero.

decimal.ROUND_FLOOR Round towards -Infinity.

decimal.ROUND_HALF_DOWN Round to nearest with ties going towards zero.

decimal.ROUND_HALF_EVEN Round to nearest with ties going to nearest even integer.

decimal.ROUND_HALF_UP Round to nearest with ties going away from zero.

decimal.ROUND_UP Round away from zero.

decimal.ROUND_05UP Round away from zero if last digit after rounding towards zero would have been 0 or 5; otherwise round towards zero.

有一种更简单的通用方法,即在四舍五入之前减去少量,如下所示:

a = 28.269
digits = 2
print(round(a - 0.5/10**digits, digits))

这是基于一种直觉,即一种将浮点数四舍五入为最接近整数的方法是加 0.5,然后截断。上面的解决方案通过减去所需精度允许的最小 'tick' 的一半,然后四舍五入来做相反的事情。

这是一个不受浮点精度错误影响的简单函数

def truncate_float(n, places):
    return int(n * (10 ** places)) / 10 ** places

测试:

>>> truncate_float(28.266, 3)
28.266
>>> truncate_float(28.266, 2)
28.26
>>> truncate_float(28.266, 1)
28.2

使用 Python 3 你可以使用 quantize()

from decimal import *    
>>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
Decimal('7.32')

您可以在代码中使用的简单函数。此函数也可用于整数楼层数。

import math
def floorDecimal(number, decimal):
    return math.floor(number * pow(10, decimal))/pow(10, decimal)

使用示例:

number = 256.789
newNumber = floorDecimal(number, 2) # newNumber is 256.78
newNumber = floorDecimal(number, -2) # newNumber is 200

这是我使用 f 作为浮点数和 d 作为小数位数的函数

from math import floor

def floor_decimal(f, d):
    n = 10 ** d
    return floor(f * n) / n