Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Psycopg2 query returning Decimal('value')

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?

like image 938
tauren.kristich Avatar asked Oct 25 '14 01:10

tauren.kristich


1 Answers

what you're seeing is the repr() of a decimal.Decimal() value, which can precisely represent decimal values, a feature not available to floats. 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'
like image 69
SingleNegationElimination Avatar answered Sep 17 '22 00:09

SingleNegationElimination