473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Money data type in PostgreSQL?

What do people recommend for storing money amounts? I've seen people use
NUMERIC(18,3) and other use NUMERIC(18,4). Which one is more appropriate
and why? This is considering various existing currencies, some having
low rates (like IDR, in which you can have large amount up to hundreds
of trillions) and some high rates (like USD, in which you can have small
amount like 0.1 cent). Are there places/industries which involve values
lower than 0.1 cent?

And what about 'factor' field in currency conversion table? Should I use
REAL, or DOUBLE PRECISION (do we need 15-16 digit precision?) or NUMERIC
(exact numbers). The factor should range between 1E-3 (e.g. converting
IDR to USD) to 1E4 (e.g. converting IDR to pounds/euros).

--
dave


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #1
12 17848
On Wed, 2003-12-03 at 07:02, David Garamond wrote:
What do people recommend for storing money amounts? I've seen people use
NUMERIC(18,3) and other use NUMERIC(18,4). Which one is more appropriate
and why? This is considering various existing currencies, some having
low rates (like IDR, in which you can have large amount up to hundreds
of trillions) and some high rates (like USD, in which you can have small
amount like 0.1 cent). Are there places/industries which involve values
lower than 0.1 cent?
I think you should match the customer's data and use whatever precision
is necessary for it. The needs of a small shop will not be the same as a
currency trader's.

You should not regard amounts in different currencies as equivalent.
You cannot add Euros to dollars and get a meaningful figure; so they
should not be in the same column. If you are handling multiple
currencies, your database design needs to be a lot more sophisticated
than having a single money column.
And what about 'factor' field in currency conversion table? Should I use
REAL, or DOUBLE PRECISION (do we need 15-16 digit precision?) or NUMERIC
(exact numbers). The factor should range between 1E-3 (e.g. converting
IDR to USD) to 1E4 (e.g. converting IDR to pounds/euros).


You should only use NUMERIC for money; any kind of floating point
representation will lose detail somewhere along the line. (I suppose
you could use BIGINT for Japanese Yen.)

--
Oliver Elphick Ol************@lfix.co.uk
Isle of Wight, UK http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"What shall we then say to these things? If God be for
us, who can be against us?" Romans 8:31
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #2
Oliver Elphick wrote:

You should not regard amounts in different currencies as equivalent.
You cannot add Euros to dollars and get a meaningful figure; so they
should not be in the same column.
I plan to store amount in a column (NUMERIC) and currency id in another
(CHAR(3)). Plus another column for amount in 'standard' currency (e.g.
USD; all addition/sum will be done to this column).
You should only use NUMERIC for money; any kind of floating point
representation will lose detail somewhere along the line. (I suppose
you could use BIGINT for Japanese Yen.)


--
dave

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #3
Funny you mention IDR-- I happen to be in Jakarta at the moment.

Obviously different customers have different requirements, but I always
suggest trading performance for usefulness where necessary.

Obviously, the scale will need to be in accordance with the needs of your
customer (unlikey to need any for IDR; 2, 3, or more depending on industry
for USD). I would also always suggest overshooting the precision by a few
places to ensure that:
1) if there is another banking crisis, your app still performs
2) take into account future inflation
3) take into account future growth.

Best WIshes,
Chris Travers
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #4
David Garamond wrote:
I plan to store amount in a column (NUMERIC) and currency id in another
(CHAR(3)). Plus another column for amount in 'standard' currency (e.g.
USD; all addition/sum will be done to this column).


If your system really is going to handle multiple, simultaneous currencies,
beware of constant changes in the exchange rate between them. Probably
you'll be better by never storing anything in a 'standard currency' column
and doing instead all math on the fly, referring to a separate
'exchange_rates' table when needed (i.e. always :-). Of course, all of this
has nothing to do with the technical solution but instead with the bussiness
rules the application must follow, so they must be incorporated early at the
spec level. In the end, probably an accountant will be the most qualified
one to define these things.

With regard to precision, it is common in certain applications the need to
handle very small amounts, especially when used as factors of a larger
calculation. I've even seen once some rates defined in hundredths of cents!

hth
cl.
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #5
Martha Stewart called it a Good Thing when cl******@hotmail.com ("Claudio Lapidus") wrote:
With regard to precision, it is common in certain applications the need to
handle very small amounts, especially when used as factors of a larger
calculation. I've even seen once some rates defined in hundredths of cents!


Well, you don't need terribly much precision in the currency exchange
rate if the amount being converted is small. You only need a couple
significant digits to convert $0.24 USD to the appropriate amount in
$CDN.

But to get the pennies right on a $10,000 USD transaction converted
into GBP (UK Pounds), you need all the official precision that there
is. And if your calculation is off by 4 cents, some of those
accounting folk are liable to thrash you mercilessly over it. If you
get calculations WRONG, they get really uncomfortable, and want to
know why.
--
(reverse (concatenate 'string "ac.notelrac.teneerf" "@" "454aa"))
http://www3.sympatico.ca/cbbrowne/li...ributions.html
"Women who seek to be equal to men lack ambition. "
-- Timothy Leary
Nov 12 '05 #6

"Claudio Lapidus" <cl******@hotmail.com> writes:
With regard to precision, it is common in certain applications the need to
handle very small amounts, especially when used as factors of a larger
calculation. I've even seen once some rates defined in hundredths of cents!


Normally there's nothing smaller than a tenth of a cent in US currency. It's
called a "mill". (or "mil"? I forget.). Of course you have to multiply
currency amounts by floating point numbers like interest rates or such, and
that will produce strange numbers but they'll always be rounded off to at
least mills and usually cents. You never actually debit or credit partial
mills.

At least that's how I was taught it was supposed to work.
I'm sure someone somewhere isn't following it.

--
greg
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #7
Christopher Browne wrote:
But to get the pennies right on a $10,000 USD transaction converted
into GBP (UK Pounds), you need all the official precision that there
is. And if your calculation is off by 4 cents, some of those
accounting folk are liable to thrash you mercilessly over it. If you
get calculations WRONG, they get really uncomfortable, and want to
know why.


What I have done is store the currency amounts as bigints, at the same
precision defined for the currency (ie cents for dollars, pence for
pounds, etc). This guarantees that you don't get any rounding errors
when storing the figures as a floating point type. When manipulating the
numbers, I use Java BigDecimals, which don't lose any precision either,
and convert back to bigints to store in the database.

YMMV.

Regards,
Graham
--
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #8
Graham Leggett wrote:
Christopher Browne wrote:
But to get the pennies right on a $10,000 USD transaction converted
into GBP (UK Pounds), you need all the official precision that there
is. And if your calculation is off by 4 cents, some of those
accounting folk are liable to thrash you mercilessly over it. If you
get calculations WRONG, they get really uncomfortable, and want to
know why.


What I have done is store the currency amounts as bigints, at the same
precision defined for the currency (ie cents for dollars, pence for
pounds, etc). This guarantees that you don't get any rounding errors
when storing the figures as a floating point type. When manipulating the
numbers, I use Java BigDecimals, which don't lose any precision either,
and convert back to bigints to store in the database.


You won't get any rounding errors in NUMERIC either. What people should
be concerned of is to find an arbitrary precision package for the
frontend programming language they're using.
Jan

--
#================================================= =====================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================= = Ja******@Yahoo.com #
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #9
On Thu, 4 Dec 2003, Jan Wieck wrote:
Graham Leggett wrote:
Christopher Browne wrote:

What I have done is store the currency amounts as bigints, at the same
precision defined for the currency (ie cents for dollars, pence for
pounds, etc). This guarantees that you don't get any rounding errors
when storing the figures as a floating point type. When manipulating the
numbers, I use Java BigDecimals, which don't lose any precision either,
and convert back to bigints to store in the database.


You won't get any rounding errors in NUMERIC either. What people should
be concerned of is to find an arbitrary precision package for the
frontend programming language they're using.


I agree, I use BigDecimal's in Java, and NUMERIC's in PostgreSQL, they
seem like a perfect match. Floating point numbers are not suitable for
money in my opinion.
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #10
Jan Wieck wrote:
You won't get any rounding errors in NUMERIC either. What people should
be concerned of is to find an arbitrary precision package for the
frontend programming language they're using.


What is the definition of a numeric number? I understand (from studying
numeric methods all those years ago) that the base 10 decimal number 0.1
cannot be stored exactly in base 2 floating point, thus my use of
integers - is numeric an arbitrary precision concept?

Regards,
Graham
--
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #11
It's probably in the documentation, but numeric is stored as a string of
digits (or was it mibbles). In any case, if you wanted to list the weight of
the earth to 20 decimal places, numeric is for you. If you like you can
consider it integer arithmatic except the scaling is handled for you.

The format is NUMERIC(x,y) where x is the total number of digits and y is
the number of decimal places. There are rules about the results of
multiplication and division and such.

Hope this helps,

On Fri, Dec 05, 2003 at 02:39:42PM +0200, Graham Leggett wrote:
Jan Wieck wrote:
You won't get any rounding errors in NUMERIC either. What people should
be concerned of is to find an arbitrary precision package for the
frontend programming language they're using.
What is the definition of a numeric number? I understand (from studying
numeric methods all those years ago) that the base 10 decimal number 0.1
cannot be stored exactly in base 2 floating point, thus my use of
integers - is numeric an arbitrary precision concept?

Regards,
Graham
--


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


--
Martijn van Oosterhout <kl*****@svana.org> http://svana.org/kleptog/ "All that is needed for the forces of evil to triumph is for enough good
men to do nothing." - Edmond Burke
"The penalty good people pay for not being interested in politics is to be
governed by people worse than themselves." - Plato


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE/0H6/Y5Twig3Ge+YRAqHqAJ9rB5VWdXEvTg+/e5/rfeMD5IOiowCfeK5f
aBz+h6AkfQdep888hdxkuTw=
=dxy7
-----END PGP SIGNATURE-----

Nov 12 '05 #12
Graham Leggett wrote:
Jan Wieck wrote:
You won't get any rounding errors in NUMERIC either. What people should
be concerned of is to find an arbitrary precision package for the
frontend programming language they're using.


What is the definition of a numeric number? I understand (from studying
numeric methods all those years ago) that the base 10 decimal number 0.1
cannot be stored exactly in base 2 floating point, thus my use of
integers - is numeric an arbitrary precision concept?


The PostgreSQL datatype NUMERIC is performing decimal arithmetic. The
original I wrote used to do it string based, with one digit per byte but
stored the number as some sort of BCD, one digit per nibble. Tom Lane
changed that a while back into base 10,000 storage and calculation,
which has the advantages of doing 4 digits per loop and no need to
convert back and forth between the storage and the computational
representation.
Jan

--
#================================================= =====================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================= = Ja******@Yahoo.com #
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Batista, Facundo | last post by:
Can't find it. I mean something like: >> m1 = Money(decimal=2) >> m2 = Money(decimal=2) >> m1.value = 3.20 >> m2.value = 2.15 >> print m1 + m2 5.35
24
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing the code. Thank you. .. Facundo
1
by: Jason Szotak | last post by:
Recently we upgraded the .Net Framework to v1.1. All of a sudden all of our queries run through .Net pages began showing all 4 of the decimal places for the money data type. Queries run through asp...
0
by: Sam | last post by:
I am trying to use a Simple form with 3 fields from SQL NorthWind Database (Order Details Table with 3 Fields. - OrderId, ProductId and Unit Price). The Field Unit Price has a data type of 'Money...
0
by: Sam | last post by:
Folks.. I am trying to use a Simple form with 3 fields from SQL NorthWind Database (Order Details Table with 3 Fields. - OrderId, ProductId and Unit Price). The Field Unit Price has a data...
3
by: Stephan Diehl | last post by:
Hi lazyweb, I'm wondering, if there is a usable money data type for python available. A quick search in pypi and google didn't convey anything, even though the decimal data type seemed to be...
2
by: thesti | last post by:
hi, i want to ask about money datatype, what makes it special or different that we better use it for a currency related field (like the price of a product) instead of using just, say a bigint or a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.