I have a JSON output that is Decimal('142.68500203')
.
I can use str() to get a string which is "142.68500203"
. But I want to get a number output which is 142.68500203
.
How to make the conversion in Python?
I have a JSON output that is Decimal('142.68500203')
.
I can use str() to get a string which is "142.68500203"
. But I want to get a number output which is 142.68500203
.
How to make the conversion in Python?
It depends what you mean by "number", it could be argued that Decimal
is a number class. If you mean into an object of float
class then:
from decimal import Decimal
x = Decimal('142.68500203')
y = float(x)
print x, y
Gives:
142.68500203 142.68500203
But beware! There are good reasons for using Decimal
, read the Decimal documentation before deciding you want float
.