[PyGreSQL] Binding decimal.Decimal

Christopher Sean Hilton chris at vindaloo.com
Tue Sep 9 09:29:02 EDT 2008


On Sep 8, 2008, at 9:59 PM, Gregory Golberg wrote:

> Hi all,
>
> It appears that binding decimal.Decimal does not work (have to use  
> int or float). Are there any plans to fix this? Using 'float' is  
> wrong in general, you don't want $10.37 to become 10.37000000001 for  
> instance...
>
> The error I get is:
>
> Native error: ("error 'do not know how to handle type <class  
> 'decimal.Decimal'>' in 'INIT'",)
>


Pardon my naivety but I'm truly curious about the solution to this  
one. I can see storing the value as a float and keeping the precision  
and scale in the class instance. That allows you to force rounding  
after internal calculations by pushing the value through a format  
mask. e.g.

     class SqlDecimal(SqlDoublePrecision):
         def __init__(self, value, precision, scale):

             SqlDoublePrecision.__init__(self, value...)
             self.Value = float(value)
             self.FormatMask = "%%%d.%df" % (precision, scale)

         def __str__(self):
             return self.FormatMask % self.Value

But that seems to ignore the problem that an SQL Decimal value can  
store much larger numbers than a IEEE Double Precision. Would this be  
done with Binary coded Decimal or pure strings instead?

-- Chris    

Chris Hilton                                   e: chris|at|vindaloo| 
dot|com
----------------------------------------------------------------------------
                   "The pattern juggler lifts his hand; The orchestra  
begin.
       As slowly turns the grinding wheel in the court of the crimson  
king."
                                            -- Ian McDonald / Peter  
Sinfield





More information about the PyGreSQL mailing list