Section 5.8 From the Java Library java.text.NumberFormat
Although the Math.round()
method is useful for rounding numbers, it is not suitable for business applications. Even for rounded values, Java will drop trailing zeroes. So a value such as $10,000.00 would be output as $10000.0. This wouldn't be acceptable for a business report.
Fortunately, Java supplies the java.text.NumberFormat
class precisely for the task of representing numbers as dollar amounts, percentages, and other formats (FigureĀ 5.8.1).
The NumberFormat
class is an abstract
class, which means that it cannot be directly instantiated. Instead, you would use its static getInstance()
methods to create an instance that can then be used for the desired formatting tasks.
Once a NumberFormat
instance has been created, its format()
method can be used to put a number into a particular format. The setMaximumFractionDigits()
and setMaximumIntegerDigits()
methods can be used to control the number of digits before and after the decimal point.
For example, the following statements can be used to format a decimal number as a currency string in dollars and cents:
NumberFormat dollars = NumberFormat.getCurrencyInstance();
System.out.println(dollars.format(10962.555));
These statements would cause the value 10962.555 to be shown as $10,962.56. Similarly, the statements,
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMaximumFractionDigits(2);
System.out.println(percent.format(6.55));
would display the value 6.55 as 6.55%. The utility of the Math
and NumberFormat
classes illustrates the following principle:
Principle 5.8.2. EFFECTIVE DESIGN: Code Reuse.
Often the best way to solve a programming task is to find the appropriate methods in the Java class library.
Subsection 5.8.1 Self-Study Exercise
-
A Certificate of Deposit (CD) is an investment instrument that accumulates interest at a given rate for an initial principal over a fixed number of years. The formula for compounding interest is shown in TableĀ 5.8.3.
Table 5.8.3. Formula for calculating compound interest \(a = p(1 + r)^n\) where \(\bullet\) a is the CD's value at the end of the nth year \(\bullet\) p is the principal or original investment amount \(\bullet\) r is the annual interest rate \(\bullet\) n is the number of years or the compounding period It assumes that interest is compounded annually. For daily compounding, the annual rate must be divided by 365, and the compounding period must be multiplied by 365, giving: \(a = p(1 + r/365)^{365n}\text{.}\)
Implement a
BankCD
class that calculates the maturity value of a CD. FigureĀ 5.8.4 gives the design of the class. It should have three instance variables for the CD's principal, rate, and years. Its constructor method sets the initial values of these variables when aBankCD
is instantiated. Its two public methods calculate the maturity value using yearly and daily compounding interest, respectively. Use theMath.pow()
method to calculate maturity values. For example, the following expression calculates maturity value with annual compounding:principal * Math.pow(1 + rate, years)
-
Design a command-line user interface to the
BankCD
class that lets the user input principal, interest rate, and years, and reports the CD's maturity value with both yearly and daily compounding. UseNumberFormat
objects to display percentages and dollar figures in an appropriate format. The program's output should look something like the following (user's inputs follow the '>' prompt):************************ OUTPUT ******************** Compare daily and annual compounding for a Bank CD. Input CD initial principal, e.g. 1000.55 > 2500 Input CD interest rate, e.g. 6.5 > 7.8 Input the number of years to maturity, e.g., 10.5 > 5 For Principal = 2,500.00 Rate= 7.8% Years= 5.0 The maturity value compounded yearly is3,639.43 The maturity value compounded daily is: $3,692.30 ************************ OUTPUT ********************