dividing money by money

Lists: pgsql-bugs
From: Andy Balholm <andy(at)balholm(dot)com>
To: pgsql-bugs(at)postgresql(dot)org
Subject: dividing money by money
Date: 2010-03-29 23:37:00
Message-ID: 31F5F44C-0614-4CDD-ABCA-889DE778EF13@balholm.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

When I divide a money value by another money value, I get an error message, as follows:

***************************
psql (8.4.1)
Type "help" for help.

postgres=# select version();
version
--------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 8.4.1 on i386-apple-darwin9.8.0, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5490), 32-bit
(1 row)

postgres=# select '$2'::money / '$1'::money;
ERROR: operator does not exist: money / money
LINE 1: select '$2'::money / '$1'::money;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
***************************

I expected to get a result of 2 or 2.0 in some numeric type (maybe double precision). The result should be a pure number because the units (dollars, in this case) cancel out.

The ability to divide money by money would be useful for finding what percent one money value is of another. That is what I was wanting to use it for—finding out what percentage of a customer's original balance has been paid off.

It would also provide a better way to convert money into numeric types than the regular expression in the documentation. You could just divide the money amount by '1'::money.

Andy Balholm
(509) 276-2065
andy(at)balholm(dot)com