I have a query in Python querying a postgresql database (via psycopg2). My code is returning the value as Decimal('VALUE'). Is there any way to convert this in the database? Code Sample:
query = "SELECT id FROM table;"
cur.execute(query)
results = cur.fetchall()
# Results = Decimal('some value')
Is there anyway to convert this to just show the value without the "Decimal()"? Maybe I have my schema set up wrong?
what you're seeing is the repr()
of a decimal.Decimal()
value, which can precisely represent decimal values, a feature not available to float
s. There's no loss of utility, though, if you want to see a nice string representation, say, to 6 decimal places, just format it the way you would a float
, with str
or format
!
In [7]: str(decimal.Decimal('0.1'))
Out[7]: '0.1'
In [13]: "{0:0.6f}".format(decimal.Decimal('0.1'))
Out[13]: '0.100000'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With