如何将Decimal()转换为Python中的数字?

时间:2016-06-17 06:52:02

标签: python

我有一个Decimal('142.68500203')的JSON输出。

我可以使用str()来获取"142.68500203"的字符串。但我希望得到一个数字输出142.68500203

如何在Python中进行转换?

2 个答案:

答案 0 :(得分:3)

这取决于你的意思"数字",可以说Decimal是一个数字类。如果你的意思是float类的对象,那么:

from decimal import Decimal

x = Decimal('142.68500203')

y = float(x)

print x, y

给出:

142.68500203 142.68500203

但要注意!在决定您需要Decimal之前,有充分的理由使用float,阅读the Decimal documentation

答案 1 :(得分:1)

这很容易:

float("142.68500203")