• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data types for money  RSS feed

 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In enterprise Java programs how are money values (dollars and cents) represented in REAL code? As cents in primitive integrals? As dollars and cents in BigDecimal (exploiting its fixed point arithmetic capability ?
Thank you.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graeme,
I like BigDecimals best since they represent the actual number and can be used in arithmetic. I've also seen storing it in a String (using 1234.56 format), but this is awkward.
 
Bob Sherry
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How you represent a money value depends on what operation you want to perform on it and what the range of values that you expect to be holding. There may also be space considerations. For example, if we are doing an inventory system, and the price of a typical item is around 5 dollars, I would think int would be a good choice. In this case, you would store 5 dollars as 500.

Right now, I am working on a retirement calculator and I am storing dollar amounts as doubles. However, floating point operations are only approximations and therefore you may end up with something like:
2.0 + 2.0 = 3.9999999
For certain applications, that can be a killer. Using a class like BigInteger or BigDecimal is also a good choice for certain applications. However, operations on these data types is going to much slower than using int or float.

Bob
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you only do addition and subtraction, and multiplication with integers only, you can easily use an int / long to represent the number of cents, and use NumberFormat for formatting. As soon as you'll need better precision (like when calculating interest etc) BigDecimal is the way to go.
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All ,except Jeanne, should please read the question.
It is not a question about Java data types but about what REAL world Java programmers do.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What people are trying to tell you is that there is not ONE DEFINITIVE answer. like almost everything in software design, the solution depends on the specific situation. There are tradeoffs you almost always have to make.

about the only constant is that you always want to store your values in the atomic unit - in the U.S., that would be cents.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Graeme Byers:
All ,except Jeanne, should please read the question.
It is not a question about Java data types but about what REAL world Java programmers do.

And that's what I said. If I don't need fractional cents I'd go for an int or long, since that's faster and less memory consuming than BigDecimal. But if fractional cents are possible, then BigDecimal is the only one that is precise enough to use; you don't want people losing money (although it's "only" cents) so float and double are out of the question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!