• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling currency throughout an application  RSS feed

 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am developing a web app that deals quite a bit with currency (2 decimal places) and rates (5 decimal places). I know about using BigDecimal to perform my calculations.

My question is in regards to representing the currency and rate in my beans. I could use String variables and BigDecimal when I want to perform calculations. Alternatively, I could create a Money and Rate class that stores the values as longs and maintains the precision. For the latter, I would need to write methods for maintaining the appropriate value when rounding becomes necessary (i.e. Money(".25") * Money(".35") = .0675, which should store 7 in the amount of my Money object.

Which of the above techniques would you use?

Does anyone else perform alot of operations involving currency? If so, do you use a total different technique?

Any help is appreciated.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using BigDecimal with a MathContext to control rounding? I've never had to use them together but the doc makes it look like what you need.

I'd probably try to handle money all as integers and format the dollars and cents when necessary. When you start doing math with other precisions - your rate with 5 decimal points - it might get a bit tricky. A money class might be a good abstraction.

Yet another reason to miss COBOL.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Yet another reason to miss COBOL.


what are we up to, now, two of them?

personally, i did kind of like COBOL's multiple-variable, multiple-condition conditional statement. made my code look like a big truth table. but other than that there wasn't much in the language to miss, frankly.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using BigDecimal with a MathContext to control rounding?


Given that this is the first time I've seen the term MathContext, no, I am not using that We use:



to control rounding.

In the beans, we have decided to implement Money and Rate classes, which essentially wrap BigDecimal objects.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Samuel Cox:
Given that this is the first time I've seen the term MathContext, no, I am not using that
According to the JavaDocs, MathContext is an immutable object that stores a precision and rounding mode pair. You can then use it when constructing new BigDecimals to make using consistent values across your application easier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!