简体   繁体   中英

Control decimal division precision in MySQLdb

MySQLdb does some weirdness where it seems to always return a Decimal object with 2 more significant figures than the numerator of a division operation.

If the denominator is relatively large, this means that sometimes the result gets truncated to zero:

>>> import MySQLdb
>>> db = MySQLdb.connect(.....)
>>> c = db.cursor();
>>> c.execute("select 1000/ 20990933630")
1L
>>> c.fetchall()
((Decimal("0.0000"),),)
>>> c.execute("select 1000.0000000000000000000000/ 20990933630")
1L
>>> c.fetchall()
((Decimal("4.763961516084313397E-8"),),)
>>> c.execute("select (1000 + 0.000000000000000000000) / 20990933630")
1L
>>> c.fetchall()
((Decimal("4.76396151608431340E-8"),),)

Can I force float division? Is there a more elegant way of doing this than adding 0.0000000000 to everything?

You can change the div_precision_increment variable. It defaults to 4.

http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_div_precision_increment

Here's an example using your division:

mysql> select 1000/ 20990933630;
+-------------------+
| 1000/ 20990933630 |
+-------------------+
|            0.0000 | 
+-------------------+
1 row in set (0.00 sec)

mysql> set local div_precision_increment = 30;
Query OK, 0 rows affected (0.00 sec)

mysql> select 1000/ 20990933630;
+----------------------------------+
| 1000/ 20990933630                |
+----------------------------------+
| 0.000000047639615160843133969739 | 
+----------------------------------+
1 row in set (0.00 sec)

mysql> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM