我需要将值舍入到正好2个小数,但是当我使用round()时它不起作用,如果值为0.4,但我需要0.40.
round(0.4232323, 2) = 0.42
bad: round(0.4, 2) = 0.4
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
我知道浮点不准确,并且想知道获得的最佳方法, 0.08354
而不是(0.08353999999999999)
当我在 Python 中执行以下操作时:
d = 8.354/100
print (d)
Run Code Online (Sandbox Code Playgroud) 我有以下形式的打印声明:
print("a = ", a, "b = ", b, "c = ", c)
Run Code Online (Sandbox Code Playgroud)
其中 a、b 和 c 是浮点数。我想打印到小数点后三位,并保留打印语句当前的形式,如果可能的话?
试图
在类似的帖子之后,我尝试了以下操作:
print(" %.3f a = ", a, "%.3f b = ", b, "%.3f c = ", c)
Run Code Online (Sandbox Code Playgroud)
但这只是在打印语句中打印了“%.3f”。关于如何调整我的打印报表有什么建议吗?
我在 Python 中遇到了浮动问题...
我在一个变量中有这个:
i = 9.600000000000001
Run Code Online (Sandbox Code Playgroud)
我会这样改造:
i = 9.60000
Run Code Online (Sandbox Code Playgroud)
小数点后有五个数字并四舍五入。
python floating-point precision floating-accuracy python-3.x
可能重复:
python限制浮动到两个小数点
如果我这样做:12.45-12
在Python中我得到的答案是:0.44999999999999929
我是这么做的:0.45?
顺便说一下,我确实记得这样做:12.45-12.0但没有结果.
>>> import numpy as np
>>> a=np.arange(0,2,0.2)
>>> a
array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8])
>>> a=a.tolist()
>>> a
[0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0, 1.2000000000000002, 1.4000000000000001, 1.6, 1.8]
>>> a.index(0.6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 0.6 is not in list
Run Code Online (Sandbox Code Playgroud)
似乎列表中的某些值已更改,我无法找到它们index()
.我该如何解决这个问题?
def introduction():
print("This calculator calculates either the Simple or Compound interest of an given amount")
print("Please enter the values for principal, annual percentage, number of years, and number of times compounded per year")
print("With this information, we can provide the Simple or Compound interest as well as your future amount")
def validateInput(principal, annualPercntageRate, numberOfYears,userCompound):
if principal < 100.00:
valid = False
elif annualPercntageRate < 0.001 or annualPercntageRate > .15:
valid = False
elif numberOfYears < 1:
valid = False …
Run Code Online (Sandbox Code Playgroud) 我在python中生成或获取小数值时遇到问题.现在,我使用float(),但它返回.0而不是.00 ...
>>> d = 10
>>> float(d)
10.0
>>>
Run Code Online (Sandbox Code Playgroud)
如何设置以.00返回的十进制值?
>>10.00
Run Code Online (Sandbox Code Playgroud)
提前致谢 ...
python ×8
decimal ×2
python-3.x ×2
arrays ×1
division ×1
list ×1
numpy ×1
precision ×1
python-2.7 ×1