Lesson 34Using the BigDecimal Class
In this lesson, we will look at the BigDecimal class. This class is used to represent arbitrary precision decimal numbers. It provides ways to set the number of significant digits and configure how numbers should be rounded. This class should be used for all calculations involving currency.
Like some of the other classes and libraries we have covered, we will introduce you to BigDecimal and show what you need to get started. We won't cover every capability of the class, but we will give you enough information so that you can explore the remaining capabilities on your own.
EXPLORING BIGDECIMAL
The Javadoc documentation for BigDecimal is pretty technical and can be a bit intimidating. In this section, we explore some of the ideas behind BigDecimal, and in the next section, we look at some code samples.
BigDecimal is an immutable type (just like String). That means that a BigDecimal object's value cannot be changed once it is set. From a practical standpoint, that means you must set the result of any BigDecimal operation to another BigDecimal variable. We will see examples of this in the code examples later in this lesson.
Constructing BigDecimals
There are several different constructors that you can use to create instances of BigDecimal. Most of them are straightforward, ...
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