Do you know Bill Karwin's joke about handling money with FLOAT? No? Then you should come to my talk. Most of the systems we develop deal with monetary amounts, and yet we know so little about money handling intricacies. This can lead to serious bugs costing millions of dollars. I will share my experience with the Java Money standard and talk about developing complex e-commerce and payment systems.
6. “A large proportion of the computers in this world
manipulate money, so it's always puzzled me that
money isn't actually a first class data type in any
mainstream programming language. The lack of a
type causes problems, the most obvious surrounding
currencies. (...) The more subtle problem is with
rounding. Monetary calculations are often rounded to
the smallest currency unit. When you do this it's easy to
lose pennies (or your local equivalent) because of
rounding errors.
The good thing about object-oriented programming is
that you can fix these problems by creating a Money
class that handles them. Of course, it's still surprising
that none of the mainstream base class libraries
actually do this.”
https://martinfowler.com/eaaCatalog/money.html
8. “Monetary values are a key feature of many applications, yet the JDK
provides little or no support. The existing java.util.Currency class is
strictly a structure used for representing current ISO-4217 currencies,
but not associated values or custom currencies. The JDK also provides
no support for monetary arithmetic or currency conversion, nor
for a standard value type to represent a monetary amount.”
https://download.oracle.com/otndocs/jcp/money_currency-1_0-fr-eval-spec/
9. public final class Money implements MonetaryAmount,
Comparable<MonetaryAmount>, Serializable {
/**
* The currency of this amount.
*/
private final CurrencyUnit currency;
/**
* the {@link MonetaryContext} used by this instance, e.g. on division.
*/
private final MonetaryContext monetaryContext;
/**
* The numeric part of this amount.
*/
private final BigDecimal number;
/* ... */
}