Re: How to sum monetary variables

From: Chris Travers <chris(dot)travers(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Martín Marqués <martin(dot)marques(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to sum monetary variables
Date: 2011-07-19 00:34:21
Message-ID: CAKt_ZfvsJHDMfgvuQaASiQ9mxR4Tu0fRF6s31-NRXXR9v4cj7Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2011/7/18 Merlin Moncure <mmoncure(at)gmail(dot)com>:
> 2011/7/18 Martín Marqués <martin(dot)marques(at)gmail(dot)com>:
>> I'm building a table (which is a report that has to be printed) with a
>> bunch of items (up to 300 in some cases) that have unitary price
>> (stored in a numeric(9,2) field), how many there are, and the total
>> price for each item. At the end of the table there is a total of all
>> the items.
>>
>> The app is running on PHP and PG is the backend.
>>
>> The question is, how do I get the total of everything?
>>
>> Running it on PHP gives one value, doing a sum() on the backend gives
>> another, and I'm starting to notice that even using python as a
>> calculator gives me errors (big ones). Right now I'm doing the maths
>> by hand to find out who has the biggest error, or if any is 100%
>> accurate.
>
> I wouldn't even bother testing client implementations that are using
> floating point numbers to do the math -- they are going to be wrong.
> If you snoop around you should find math libraries that handle do a
> better job handling exact numbers for most popular languages.  A few
> languages, like the hated COBOL, have them built in.
>
All my testing has suggested that Perl number addition, etc. is
arbitrary precision safe. (I usually use additional libraries for
finer control though).

I don't know about PHP.

Best Wishes,
Chris Travers

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Chris Travers 2011-07-19 00:35:46 Re: How to sum monetary variables
Previous Message Craig Ringer 2011-07-18 23:56:15 Re: Alarm function in PL/pgSQL