8. Java Data and Operators

8.6. From the Java Library

java.text.NumberFormat

ALTHOUGH the Math.round() method is useful for rounding num-


 

 

 

 

 

Figure 5.9: The NimPlayer class.

 

java.sun.com/j2se/1.5.0/docs/api/

 

bers, 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 pre- cisely 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:

,,

 

J

These statements would cause the value 10962.555 to be shown as

$10,962.56. Similarly, the statements,

,,

 

 

J

would display the value 6.55 as 6.55%. The utility of the Math and

NumberFormat classes illustrates the following principle:

 

 

SELF-STUDY EXERCISE

EXERCISE 5.7 A Certificate of Deposit (CD) is an investment instru- ment 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.11. It assumes that interest is compounded annually. For daily compounding, the annual rate must be divided by 365, and the com- pounding 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. Fig- ure 5.11 gives the design of the class. It should have three instance vari- ables 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

 

 

Figure 5.11: The BankCD class.

 

compounding interest, respectively. Use the Math.pow() method to cal- culate maturity values. For example, the following expression calculates maturity value with annual compounding:

,,

 

J

 

TABLE 5.12 Formula for calculating compound interest

a = p(1 + r)n 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

n 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 re- ports 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 ********************