我有一个Decimal('142.68500203')
的JSON输出。
我可以使用str()来获取"142.68500203"
的字符串。但我希望得到一个数字输出142.68500203
。
如何在Python中进行转换?
答案 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")