在Python中,十进制数字显示为浮点数。

huangapple go评论124阅读模式
英文:

Decimal numbers in Python are displayed as floats

问题

I have defined my variables as decimal but they are still output as float when I print them.

我已将我的变量定义为十进制,但当我打印它们时,它们仍然以浮点数输出。

My actual problem has complex calculations and I want to print the values to verify my results but I’m getting floats not decimals.

我的实际问题涉及复杂计算,我想打印这些值以验证我的结果,但我得到的是浮点数而不是十进制数。

Does this simply not matter as the calculations will come out correctly?

这是否意味着这并不重要,因为计算将会得出正确的结果?

from decimal import Decimal
from decimal import getcontext
getcontext().prec = 6

a=Decimal(0.4)
print(a)
b=Decimal(0.3)
print(b)
x=(a/b)
print("x="+str(x))

Output

输出:

0.40000000000000002220446049250313080847263336181640625
0.299999999999999988897769753748434595763683319091796875
x=1.33333

** Process exited - Return Code: 0 ** Press Enter to exit terminal

**进程已退出 - 返回代码:0 ** 按Enter键退出终端

Update

更新

This makes for some pretty silly looking code.

这使得代码看起来相当滑稽。

from decimal import Decimal
from decimal import getcontext
from random import random
getcontext().prec = 6

a=Decimal(str(random()))
print(a)
b=Decimal(str(random()))
print(b)
x=(a/b)
print("x="+str(x))

Result

结果

I still didn’t get the precision as I was expecting but I can work with that 在Python中,十进制数字显示为浮点数。

我仍然没有得到我期望的精度,但我可以接受这样的结果 在Python中,十进制数字显示为浮点数。

0.27010708573406494
0.9726705675299723
x=0.277696

** Process exited - Return Code: 0 ** Press Enter to exit terminal

**进程已退出 - 返回代码:0 ** 按Enter键退出终端

英文:

I have defined my variables as decimal but they are still output as float when I print them.

My actual problem has complex calculations and I want to print the values to verify my results but I’m getting floats not decimals.<br>
Does this simply not matter as the calculations will come out correctly?

from decimal import Decimal
from decimal import getcontext
getcontext().prec = 6

a=Decimal(0.4)
print(a)
b=Decimal(0.3)
print(b)
x=(a/b)
print(&quot;x=&quot;+str(x))

Output

0.40000000000000002220446049250313080847263336181640625
0.299999999999999988897769753748434595763683319091796875
x=1.33333


** Process exited - Return Code: 0 **
Press Enter to exit terminal

Update<br>

This makes for some pretty silly looking code.

from decimal import Decimal
from decimal import getcontext
from random import random
getcontext().prec = 6

a=Decimal(str(random()))
print(a)
b=Decimal(str(random()))
print(b)
x=(a/b)
print(&quot;x=&quot;+str(x))

Result <br>
I still didn’t get the precision as I was expecting but I can work with that 在Python中,十进制数字显示为浮点数。

0.27010708573406494
0.9726705675299723
x=0.277696

** Process exited - Return Code: 0 **
Press Enter to exit terminal

答案1

得分: 1

无法确切确定是什么导致了这个问题,但是将小数值用引号括起来解决了它:

a = Decimal("0.4")
print(a)
b = Decimal("0.3")
print(b)
x = (a / b)
print("x=" + str(x))

编辑:这可能是因为小数值是使用浮点值进行初始化的。

英文:

can't exactly figure out what's causing this issue, but wrapping the decimals around quotes solved it for me:

a=Decimal(&quot;0.4&quot;)
print(a)
b=Decimal(&quot;0.3&quot;)
print(b)
x=(a/b)
print(&quot;x=&quot;+str(x))

Edit: It's probably because the decimals are being initialized with the float values instead

huangapple
  • 本文由 发表于 2023年3月12日 13:52:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定