Re: How to use the postgresql money type?

From: Federico Di Gregorio <federico(dot)digregorio(at)dndg(dot)it>
To: psycopg(at)postgresql(dot)org
Subject: Re: How to use the postgresql money type?
Date: 2011-01-20 15:45:30
Message-ID: 4D38589A.3010506@dndg.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)tplus1(dot)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(dot)digregorio(at)dndg(dot)it
Studio Associato Di Nunzio e Di Gregorio http://dndg.it
Degli altri, della gente senza domande, si puo' fare a meno.
-- macchinavapore

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message A.M. 2011-01-20 15:50:16 Re: How to use the postgresql money type?
Previous Message A.M. 2011-01-20 15:40:19 Re: How to use the postgresql money type?