6. 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.

NumberFormat
+ getInstance():NumberFormat
+ getCurrencyInstance():NumberFormat
+ getPercentInstance():NumberFormat
+ format(in n:double):String
+ format(in n:long):String

Fortunately, Java supplies the java.text.NumberFormat class precisely for the task of representing numbers as dollar amounts, percentages, and other formats (Fig. 5.10).

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. getCurrencylnstance 0; 

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. getPercentlnstance (); 

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:

JAVA EFFECTIVE DESIGN
Code Reuse. Often the best way to solve a programming task is to find the appropriate methods in the Java class library.

SELF-STUDY EXERCISE 

EXERCISE 5.7 A Certificate of Deposit (CD) is an investment instrument that accumulates interest at a given rate for an initial principal over Figure 5.11: The BankCD class. a fixed number of years. The formula for compounding interest is shown in Table 5.11. 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 . Implement a BankCD class that calculates the maturity value of a CD. Figure 5.11 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 a BankCD is instantiated. Its two public methods calculate the maturity value using yearly and daily compounding interest, respectively. Use the Math.pow() method to calculate maturity values. For example, the following expression calculates maturity value with annual compounding:

BankCD
- principal: double
- rate: double
- years: double
+ BankCD(p:double,r:double,
y:double)
+ calcYearly():double
+ calcDaily():double

Figure 5.11: The BankCD class.

principal * Math.pow(1 + rate, years)

TABLE 5.12 Formula for calculating compound interest
\(a = p(I +r)2\) where
•a is the CD's value at the end of the nth year
•p is the principal or original investment amount
•r is the annual interest rate
•ri is the number of years or the compounding period

EXERCISE 5.8 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. Use NumberFormat objects to display percentages and dollar figures in an appropriate format. The program's output should look something like the following (user's inputs are in cyan): 

************************ 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 is $3,639.43 The maturity value compounded daily is: $3,692.30 

************************ OUTPUT ********************