[Tutor] MySQLdb: dropping zeroes!

Lloyd Kvam pythonTutor at venix.com
Wed Apr 7 08:33:25 EDT 2004


The MySQLdb module will return decimal values as Python floats.  The
floats are binary representations of the underlying decimal values and
are as accurate as possible.  All integers are exactly correct.

Remember that 1.0 == 1.00 == 1.000000

You may have a notation where the number of digits after the decimal
point represents the accuracy of a measurement, but that is not how the
computer works.

>>> "%7.4f" % 1.0
' 1.0000'
>>> "%07.4f" % 1.0
'01.0000'

You can use format strings to control how many decimal places get
displayed.

I'm not sure what your type checking is trying to do.

On Tue, 2004-04-06 at 22:04, Tim Johnson wrote:
> Hello All:
>     I'm using MySQLdb to read and write from a database where
> I have values stored as a decimal(10,2) datatype.
> To illustrate:
>   The following value which is stored as 
>     0.94
>   is read back by MySQLdb with the DictCursor option as 
>     0.94
>   so far so good!
>   But, alas! A value stored as 
>     1.00
>   is read back by MySQLdb with the DictCursor option as 
>     1.0
>  
>  That is not good! My type-checking will flag that value as invalid
>  because a precision of 2 places to the right of the decimal point
>  is expected.
> 
>  I can handle this with my own code, but perhaps someone can tell
>  me if the MySQLdb api can take care of this with some internal
>  setting. 
> 
>  TIA
>  tim
-- 

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list