W
WJ
What is the best data type for doing money type calculations, such as
compound interest and such?
compound interest and such?
WJ said:What is the best data type for doing money type calculations, such as
compound interest and such?
What is the best data type for doing money type calculations, such as
compound interest and such?
"WJ" <[email protected]> said:So you both are saying I basically have to take money values, multiply them
by 100 to get the int, manipulate them, then divide back by 100 to get the
true dollars and cents?
Hmmm. . . .I've encountered the problem with floats. Seems like there
should be an integral with an optional precision to determine how many
decimal places to the right it can support.
Just a thought. . . .
WJ said:So you both are saying I basically have to take money values, multiply them
by 100 to get the int, manipulate them, then divide back by 100 to get the
true dollars and cents?
Hmmm. . . .I've encountered the problem with floats. Seems like there
should be an integral with an optional precision to determine how many
decimal places to the right it can support.
WJ said:So you both are saying I basically have to take money values, multiply them
by 100 to get the int, manipulate them, then divide back by 100 to get the
true dollars and cents?
Jacob said:Thinking in dollars and cents just confuses you.
Program as everything is cents. Store them as long
to handle big amounts. Only in GUI you may convert
amounts to/from something more human readable.
Do not use floats or doubles or Big*.
Michael said:Bad advice. BigDecimal hast at least one crucial advantage over using
long: it allows you to use a variety if rounding modes, and in
financial applications, the rounding mode is often a hard requirement
that may even be mandated by law.
Jacob said:There will be many cases where you need to handle
fractional amounts. This must always be done with
care in order to prevent accumulated errors, and
rounding must be done correctly according to the
case at hand.
Still, when such operations has been made, always
put the result back into an integral type. That is
the only way to properly control where/when and
how rounding should be applied.
Assuming you mean Java data type, you wish to minimise inaccurateWJ said:What is the best data type for doing money type calculations, such as
compound interest and such?
David said:Assuming you mean Java data type, you wish to minimise inaccurate
results rather than memory and you don't have, or wish to write, a
specialised money data type, I suggest you use doubles.
You will find many articles on the Internet that tell you that you can
precisely represent your pennies, cents, lira etc with a long integer
and so you must use one. They ignore the fact that you may not be able
to represent an interest rate or a currency conversion as precisely.
They also ignore the fact that you must convert the "real world"
decimal data to binary.
Although your processor probably supports
decimal number calculations
(http://www.intel.com/design/Pentium4/manuals/25366515.pdf Chapter
4.7), Java does not.
Doubles cater for about 15 digits of accuracy so, assuming that your
bank account is around a million dollars and your interest rate is to
four decimal places you still have few digits to spare. There is at
least one hundred years experience behind minimising and understanding
the errors associated with floating point calculations.
Jacob said:Thinking in dollars and cents just confuses you.
Program as everything is cents. Store them as long
to handle big amounts. Only in GUI you may convert
amounts to/from something more human readable.
sks said:What happens if you use a long and you need 1 tenth of a cent ?
you use 1000 as your scale factor instead of 100.What happens if you use a long and you need 1 tenth of a cent ?
And you CANNOT EVEN REPRESENT most decimal fractions exactly with a double!
see http://mindprod.com/jgloss/currency.htmlI would think the best data type for this would be Money, or Currency, or
whatever the author of the library intended for that purpose has named the
class. I find it hard to believe that such a library does not exist.
If not, then you should probably be asking for advice on how to implement
such a class.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.