I ran into an interesting problem. I have a MySQL database that contains some doubles with very precise decimal values (for example, 0.00895406607247756, 17 decimal places). This is scientific data so this high level of precision is very important.
I'm using MySQLdb in Python to select data from the database:
cursor.execute("SELECT * FROM " ...etc...)
for n in range(cu开发者_如何学编程rsor.rowcount):
row = cursor.fetchone()
print row
For some reason, when it gets to my very precise decimals, they've been truncated to a maximum of 14 decimal places. (i.e. the previous decimal becomes 0.00895406607248)
Is there any way to get the data from MySQLdb in its original form without truncating?
What MySQL datatype are you using to store the data? Is it DECIMAL(18,17)
? DECIMAL
s have up to 65 digits of precision.
If you set the MySQL data type to use DECIMAL(...)
, then MySQLdb will convert the data to a Python decimal.Decimal object, which should preserve the precision.
精彩评论