我希望将 a
四舍五入到 13.95。我尝试使用 round
:
>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
您遇到了带有浮点数的 old problem,并非所有数字都可以精确表示。命令行只是向您显示内存中的完整浮点形式。
使用浮点表示,您的四舍五入版本是相同的数字。由于计算机是二进制的,它们将浮点数存储为整数,然后将其除以 2 的幂,因此 13.95 将以类似于 125650429603636838/(2**53) 的方式表示。
双精度数的精度为 53 位(16 位),而普通浮点数的精度为 24 位(8 位)。 floating point type in Python uses double precision 用于存储值。
例如,
>>> 125650429603636838/(2**53)
13.949999999999999
>>> 234042163/(2**24)
13.949999988079071
>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999
如果您只在小数点后两位(例如,显示货币价值),那么您有几个更好的选择:
使用整数并以美分而不是美元存储值,然后除以 100 以转换为美元。或者使用像十进制这样的定点数。
有新的格式规范,String Format Specification Mini-Language:
您可以执行以下操作:
"{:.2f}".format(13.949999999999999)
注意1:上面返回的是一个字符串。为了获得浮点数,只需用 float(...)
包装:
float("{:.2f}".format(13.949999999999999))
注意 2: 用 float()
包装不会改变任何内容:
>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
'{0:,.2f}'.format(1333.949999999)
打印 '1,333.95'
。
float()
包装它; float("{0:.2f}".format(13.9499999))
f"Result is {result:.2f}"
内置 round()
在 Python 2.7 或更高版本中运行良好。
例子:
>>> round(14.22222223, 2)
14.22
round(2.16, 1)
给出 2.2
为什么 python 只提供 truncate
函数
>>> round(2.675, 2) 2.67
docs.python.org/2/tutorial/floatingpoint.html
Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float.
这里似乎还没有人提到它,所以让我举一个 Python 3.6 的 f-string/template-string 格式的例子,我认为它非常简洁:
>>> f'{a:.2f}'
它也适用于更长的示例,使用运算符并且不需要括号:
>>> print(f'Completed in {time.time() - start:.2f}s')
我觉得最简单的方法是使用format()
函数。
例如:
a = 13.949999999999999
format(a, '.2f')
13.95
这会产生一个浮点数作为四舍五入到小数点后两位的字符串。
大多数数字不能用浮点数精确表示。如果你想对数字进行四舍五入,因为这是你的数学公式或算法需要的,那么你想使用round。如果您只想将显示限制在某个精度,那么甚至不要使用 round 并将其格式化为该字符串。 (如果你想用一些替代的舍入方法来显示它,并且有很多,那么你需要混合这两种方法。)
>>> "%.2f" % 3.14159
'3.14'
>>> "%.2f" % 13.9499999
'13.95'
最后,虽然也许最重要的是,如果你想要精确的数学,那么你根本不需要浮点数。通常的例子是处理金钱并将“美分”存储为整数。
利用
print"{:.2f}".format(a)
代替
print"{0:.2f}".format(a)
因为后者在尝试输出多个变量时可能会导致输出错误(见注释)。
试试下面的代码:
>>> a = 0.99334
>>> a = int((a * 100) + 0.5) / 100.0 # Adding 0.5 rounds it up
>>> print a
0.99
round
函数相比没有任何好处。另一方面,因为这个解决方案仍然使用浮点,所以 OP 的原始问题仍然存在,即使对于这个“解决方案”的“更正”版本也是如此。
round
函数(在问题中使用)的不必要的重新实现。
round()
不像 OP 提到的那样工作,这是必要的。
TLDR ;)
Python 3.1 彻底解决了输入和输出的舍入问题,并且该修复也向后移植到 Python 2.7.0。
四舍五入的数字可以在浮点数和字符串之间进行可逆转换:
str -> float() -> repr() -> float() ...
或 Decimal -> float -> str -> Decimal
>>> 0.3
0.3
>>> float(repr(0.3)) == 0.3
True
存储不再需要 Decimal
类型。
算术运算的结果必须再次四舍五入,因为四舍五入的误差可能会比解析一个数字后累积的误差更大。改进的 repr()
算法(Python >= 3.1, >= 2.7.0)没有解决这个问题:
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
输出字符串函数 str(float(...))
在 Python < 中被四舍五入为 12 个有效数字。 2.7x 和 < 3.1,防止过多的无效数字类似于未固定的repr()输出。减去非常相似的数字后仍然不够,并且在其他操作后四舍五入太多。 Python 2.7 和 3.1 使用相同长度的 str() 尽管 repr() 是固定的。一些旧版本的 Numpy 也有过多的无效数字,即使使用固定的 Python。当前的 Numpy 是固定的。 Python 版本 >= 3.2 与 str() 和 repr() 函数的结果相同,并且在 Numpy 中也有类似函数的输出。
测试
import random
from decimal import Decimal
for _ in range(1000000):
x = random.random()
assert x == float(repr(x)) == float(Decimal(repr(x))) # Reversible repr()
assert str(x) == repr(x)
assert len(repr(round(x, 12))) <= 14 # no excessive decimal places.
文档
见Release notes Python 2.7 - Other Language Changes第四段:
浮点数和字符串之间的转换现在在大多数平台上都可以正确舍入。这些转换发生在许多不同的地方:浮点数和复数上的 str(); float 和 complex 构造函数;数字格式;使用 marshal、pickle 和 json 模块对浮点数和复数进行序列化和反序列化;解析 Python 代码中的浮点数和虚数文字;和小数到浮点数的转换。与此相关,浮点数 x 的 repr() 现在返回基于最短十进制字符串的结果,该字符串保证在正确舍入(使用半舍入到偶数舍入模式)下舍入为 x。以前它给出了一个基于将 x 舍入为 17 位十进制数字的字符串。
更多信息: Python 2.7 之前的 float
的格式类似于当前的 numpy.float64
。两种类型都使用相同的 64 位 IEEE 754 双精度和 52 位尾数。一个很大的区别是 np.float64.__repr__
经常使用过多的十进制数进行格式化,因此不会丢失任何位,但在 13.949999999999999 和 13.950000000000001 之间不存在有效的 IEEE 754 数字。结果不是很好,并且转换 repr(float(number_as_string))
用 numpy 是不可逆的。另一方面: float.__repr__
被格式化,因此每个数字都很重要;序列没有间隙,转换是可逆的。简单地说:如果您可能有一个 numpy.float64 数字,请将其转换为普通浮点数,以便为人类格式化,而不是为数字处理器格式化,否则 Python 2.7+ 不需要更多。
float
(双精度)和普通 round
,而不是关于 numpy.double 及其转换为字符串。普通的 Python 舍入确实不能比 Python 2.7 做得更好。大多数答案都是在 2.7 之前写的,但是它们已经过时了,尽管它们最初非常好。这就是我回答的原因。
1
,“逐渐下溢”期间除外。
a*b
与 b*a
。感谢您的链接——怀旧。
float_number = 12.234325335563
round(float_number, 2)
这将返回;
12.23
解释:
round 函数有两个参数;要四舍五入的数字和要返回的小数位数。这里我返回了 2 个小数位。
您可以修改输出格式:
>>> a = 13.95
>>> a
13.949999999999999
>>> print "%.2f" % a
13.95
对于 Python < 3(例如 2.6 或 2.7),有两种方法可以做到这一点。
# Option one
older_method_string = "%.9f" % numvar
# Option two (note ':' before the '.9f')
newer_method_string = "{:.9f}".format(numvar)
但请注意,对于 3 以上的 Python 版本(例如 3.2 或 3.3),选项二是 preferred。
有关选项二的更多信息,我建议在 string formatting from the Python documentation 上使用此链接。
有关选项一的更多信息,this link will suffice and has information on the various flags。
参考:Convert floating point number to a certain precision, and then copy to string
numvar=12.456
,则 "{:.2f}".format(numvar)
产生 12.46
但 "{:2i}".format(numvar)
给出错误,我期待 12
。
您可以使用格式运算符在 python 中将值四舍五入到小数点后 2 位:
print(format(14.4499923, '.2f')) // output is 14.45
在 Python 2.7 中:
a = 13.949999999999999
output = float("%0.2f"%a)
print output
output
与 a
具有 完全相同 值,因此您不妨在最后一行写 print a
而不是 print output
。
13.95
。但是在 Python 2.7 中,对于 a
的这个特定值,print a
也是如此,所以还不清楚格式化步骤的重点是什么。
a == output
?它为我提供 True
,我怀疑它也为您提供。
正如@Matt 所指出的,Python 3.6 提供了 f-strings,他们也可以使用 nested parameters:
value = 2.34558
precision = 2
width = 4
print(f'result: {value:{width}.{precision}f}')
这将显示 result: 2.35
我们有多种选择:选项1:
x = 1.090675765757
g = float("{:.2f}".format(x))
print(g)
选项 2:内置的 round() 支持 Python 2.7 或更高版本。
x = 1.090675765757
g = round(x, 2)
print(g)
Python 教程有一个名为 Floating Point Arithmetic: Issues and Limitations 的附录。阅读。它解释了正在发生的事情以及为什么 Python 正在尽力而为。它甚至有一个与你相匹配的例子。让我引用一点:
>>> 0.1 0.10000000000000001 您可能很想使用 round() 函数将其切回您期望的单个数字。但这并没有什么区别: >>> round(0.1, 1) 0.10000000000000001 问题是存储为“0.1”的二进制浮点值已经是 1/10 的最佳二进制近似值,因此尝试再次舍入它可以不要让它变得更好:它已经和它一样好。另一个结果是,由于 0.1 不完全是 1/10,因此对 0.1 的十个值求和也可能不会产生正好 1.0: >>> sum = 0.0 >>> for i in range(10): ... sum += 0.1 ... >>> 总和 0.99999999999999989
您的问题的一种替代方案和解决方案是使用 decimal
模块。
使用 Decimal 对象和 round() 方法的组合。
Python 3.7.3
>>> from decimal import Decimal
>>> d1 = Decimal (13.949999999999999) # define a Decimal
>>> d1
Decimal('13.949999999999999289457264239899814128875732421875')
>>> d2 = round(d1, 2) # round to 2 decimals
>>> d2
Decimal('13.95')
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)
orig_float = 232569 / 16000.0
14.5355625
short_float = float("{:.2f}".format(orig_float))
14.54
为了修复 Python 和 JavaScript 等类型动态语言中的浮点,我使用了这种技术
# 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
您还可以使用十进制,如下所示:
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
仅适用于函数范围或所有地方?
像这样的 lambda 函数怎么样:
arred = lambda x,n : x*(10**n)//1/(10**n)
这样你就可以这样做:
arred(3.141591657,2)
并得到
3.14
很简单,比如 1,2,3:
使用十进制模块进行快速正确舍入的十进制浮点运算:d=Decimal(10000000.0000009)
实现四舍五入:
d.quantize(Decimal('0.01'))
将产生 Decimal('10000000.00')
使上述干燥:
def round_decimal(number, exponent='0.01'):
decimal_value = Decimal(number)
return decimal_value.quantize(Decimal(exponent))
或者
def round_decimal(number, decimal_places=2):
decimal_value = Decimal(number)
return decimal_value.quantize(Decimal(10) ** -decimal_places)
赞成这个答案:)
PS:批评他人:格式化不是四舍五入。
简单的解决方案在这里
value = 5.34343
rounded_value = round(value, 2) # 5.34
8/3
已经属于 float
类型,因此 float
调用没有任何用处。直接调用 dunder 方法有点奇怪 - 相反,只需调用委托给这些 dunder 方法的函数。所以拼写第一行的更好方法就是value = round(8/3, 2)
。到那时,您并没有真正添加其他答案中没有的任何内容。
这是使用格式功能的简单解决方案。
float(format(num, '.2f'))
注意:我们将数字转换为浮点数,因为格式方法返回字符串。
如果你想处理钱,使用 python decimal 模块
from 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+.5)/10^n 多年来为我工作了很多语言。
要将数字四舍五入为分辨率,最好的方法是以下一种,它可以用于任何分辨率(0.01 表示两位小数甚至其他步长):
>>> 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
numpy.round
准确性/精度。所以它需要在与分辨率相乘之前将其定义为 int 。我更新了代码。谢谢你!
numpy.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 的信息现在已添加到 my answer 中,您可能会误投反对票。最初与 numpy 无关。
int
您也可以使用 float
@szeitlin 示例来代替。感谢您的额外评论。 (对不起,我没有对你投反对票)
我看到的答案不适用于 float(52.15) 案例。经过一些测试,有我正在使用的解决方案:
import decimal
def value_to_decimal(value, decimal_places):
decimal.getcontext().rounding = decimal.ROUND_HALF_UP # define rounding method
return decimal.Decimal(str(float(value))).quantize(decimal.Decimal('1e-{}'.format(decimal_places)))
('value' 到 float 再到 string 的转换非常重要,这样,'value' 的类型可以是 float、decimal、integer 或 string!)
希望这对任何人都有帮助。
不定期副业成功案例分享
"%.2f" % round(a,2)
您不仅可以在 printf 中输入,还可以在str()
等内容中输入float
类型)只是十进制数的最接近的可用近似值(您作为人类所熟悉的)。没有像 0.245 这样的(有限可表示的)二进制值。它根本不存在,并且在数学上不可能存在。最接近 0.245 的二进制值略小于 0.245,所以自然会向下取整。同样,二进制中没有 0.225 这样的东西,但是最接近 0.225 的二进制值略大于 0.225,所以自然会向上取整。Decimal
,这是此答案中提出的解决方案之一。另一种是将您的数量转换为整数并使用整数算术。这两种方法也出现在其他答案和评论中。