Re: How to use the postgresql money type? - Mailing list psycopg

From Federico Di Gregorio
Subject Re: How to use the postgresql money type?
Date
Msg-id 4D38589A.3010506@dndg.it
Whole thread Raw
In response to Re: How to use the postgresql money type?  ("A.M." <agentm@themactionfaction.com>)
Responses Re: How to use the postgresql money type?
Re: How to use the postgresql money type?
List psycopg
On 20/01/2011 16:40, A.M. wrote:
>
> On Jan 19, 2011, at 6:42 PM, Daniele Varrazzo wrote:
>
>> On Wed, Jan 19, 2011 at 8:06 PM, W. Matthew Wilson <matt@tplus1.com> wrote:
>>> PostgreSQL has a money type, but I don't know how to use it with
>>> psycopg2.  Do I need  to write my own code to convert to and from SQL?
>>
>>
>> I also see that PostgreSQL doesn't let you convert from decimal to
>> money: bad stuff
>
> That's because non-integer numbers in postgresql are interpreted as floats,
> which is exactly what you do not want to represent money. It's the
same in python:

decimal is not float.

> So the example which accepts floats and converts the input to a string includes a critical bug,
> not a workaround. Because the money datatype is locale-dependent, the type should effectively
> be treated as an opaque text value- the value in the database must be used as-is for display
> to the user (including the decimal delimiter and currency indicator).
Math on money must be
> done in the database- this is required to properly handle banker's rounding and other money
> gimmicks.

But this makes sense. Do you mean that banker's rounding may depend on
the locale? If that's the case and if PostgreSQL implements such
gimmicks then the correct default for money would be to return it to
Python as string and let the user decide how to proceed from there.

federico

--
Federico Di Gregorio                         federico.digregorio@dndg.it
Studio Associato Di Nunzio e Di Gregorio                  http://dndg.it
 Degli altri, della gente senza domande, si puo' fare a meno.
                                                       -- macchinavapore

psycopg by date:

Previous
From: "A.M."
Date:
Subject: Re: How to use the postgresql money type?
Next
From: "A.M."
Date:
Subject: Re: How to use the postgresql money type?