21.7 Money and Currency
Numbers require context. A number alone without a unit and without context isn’t usable. Numbers always must describe something: an age, a tree height, a turnover, or more generally “life, the universe, and everything.”
21.7.1 Representing Amounts of Money
For money amounts, no specific data type exists in Java, and so the storage of money values can look different, depending on the program. The following data types are suitable:
-
BigDecimal : Its advantages are precise calculations and selectable roundings.
-
A pair of int or long : Enables separate storage of the digits before and after the decimal point.
-
int or long : Instead of separate dollar and cent amounts, everything is stored in cents.
[»] Note ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access