我想将a
四舍五入为13.95。我尝试使用round
,但是我得到的结果是:
>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
如果您遇到了标准库Decimal类的类似问题,请参见如何格式化小数以始终显示2个小数位?。
我想将a
四舍五入为13.95。我尝试使用round
,但是我得到的结果是:
>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
如果您遇到了标准库Decimal类的类似问题,请参见如何格式化小数以始终显示2个小数位?。
from decimal import Decimal
def round_float(v, ndigits=2, rt_str=False):
d = Decimal(v)
v_str = ("{0:.%sf}" % ndigits).format(round(d, ndigits))
if rt_str:
return v_str
return Decimal(v_str)
结果:
Python 3.6.1 (default, Dec 11 2018, 17:41:10)
>>> round_float(3.1415926)
Decimal('3.14')
>>> round_float(3.1445926)
Decimal('3.14')
>>> round_float(3.1455926)
Decimal('3.15')
>>> round_float(3.1455926, rt_str=True)
'3.15'
>>> str(round_float(3.1455926))
'3.15'
float
而不是 Decimal
?像这样:def round_float(v, ndigits=2) -> float: d = Decimal(v); v_str = ("{0:.%sf}" % ndigits).format(round(d, ndigits)); return float(v_str)
- alpervalue = 5.34343
rounded_value = round(value, 2) # 5.34
8/3
已经是float
类型,所以float
调用没有任何有用的作用。直接调用dunder方法有点奇怪-相反,只需调用委托给这些dunder方法的函数即可。因此,更好的拼写第一行的方法是简单地value = round(8/3, 2)
。在那时,您实际上并没有添加任何其他答案中不存在的内容。 - Mark Dickinson可以像这样使用lambda函数:
arred = lambda x,n : x*(10**n)//1/(10**n)
arred(3.141591657, 2)
并获取
3.14
def
编写。在这里创建一个 lambda
只是为了炫耀,并没有特别的收益。 - Karl Knechtel这很简单,就像:
use decimal module for fast correctly-rounded decimal floating point arithmetic:
d = Decimal(10000000.0000009)
to achieve rounding:
d.quantize(Decimal('0.01'))
will result with Decimal('10000000.00')
make the above DRY:
def round_decimal(number, exponent='0.01'):
decimal_value = Decimal(number)
return decimal_value.quantize(Decimal(exponent))
or
def round_decimal(number, decimal_places=2):
decimal_value = Decimal(number)
return decimal_value.quantize(Decimal(10) ** -decimal_places)
PS:对其他人的批评:格式不要圆整。
(注:此句话可能有歧义,如果需要更准确的翻译,请提供上下文。)orig_float = 232569 / 16000.0
14.5355625
short_float = float("{:.2f}".format(orig_float))
14.54
# For example:
a = 70000
b = 0.14
c = a * b
print c # Prints 980.0000000002
# Try to fix
c = int(c * 10000)/100000
print c # Prints 980
您也可以按以下方式使用Decimal:
from decimal import *
getcontext().prec = 6
Decimal(1) / Decimal(7)
# Results in 6 precision -> Decimal('0.142857')
getcontext().prec = 28
Decimal(1) / Decimal(7)
# Results in 28 precision -> Decimal('0.1428571428571428571428571429')
getcontext().prec = 6
只在函数范围内有效还是全局都有效? - Julio Marinsfrom decimal import Decimal, ROUND_HALF_UP
# 'amount' can be integer, string, tuple, float, or another Decimal object
def to_money(amount) -> Decimal:
money = Decimal(amount).quantize(Decimal('.00'), rounding=ROUND_HALF_UP)
return money
lambda x, n:int(x*10^n + 0.5)/10^n
在许多语言中,这种方法已经为我工作了很多年。
>>> import numpy as np
>>> value = 13.949999999999999
>>> resolution = 0.01
>>> newValue = int(np.round(value/resolution))*resolution
>>> print newValue
13.95
>>> resolution = 0.5
>>> newValue = int(np.round(value/resolution))*resolution
>>> print newValue
14.0
- szeitlinimport numpy as np res = 0.01 value = 0.184 np.round(value/res) * res 0.17999999999999999
numpy.round
的精度上。因此,在与分辨率相乘之前需要将其定义为整数。我已经更新了代码。谢谢! - iblasinumpy.float64
结果转换为float
,或者简单地使用round(value, 2)
。在13.949999999999999(=1395/100)和3.950000000000001(=1395*.01)之间不存在有效的IEEE 754数字。你为什么认为你的方法是最好的?原始值13.949999999999999289(= value = round(value, 2))甚至比你的13.95000000000000178(由np.float96打印)更精确。现在还为numpy添加了更多信息,可以在我的答案中找到,你可能误操作给它点了踩。它最初并不是关于numpy的。 - hynekcerfloat
而不是int
。感谢您的额外评论。(抱歉,我没有给你投反对票) - iblasi