go to previous page   go to home page   go to next page

Answer:

No. Rounding only affects the characters, not the value in the variable. value provides data when format() creates a string of characters, but value itself is not changed.

Wrapper Class Output

import java.text.*;

class IODemoWrapper
{
  public static void main ( String[] args )
  {
    Integer i = new Integer( 7654321 );
    Double  d = new Double ( 11000.0008 );
    
    DecimalFormat numform = new DecimalFormat(); 
    
    System.out.println( "integer = " + numform.format(i) + " double = " + numform.format(d) );
  }
}

Recall (from the end of chapter 9C) that a wrapper class defines objects that each hold a primitive value. For example, objects of class Integer each hold one int and some methods for manipulating ints. A wrapper object may be used as data for format().

The output of the program is (for a computer in the US):

integer = 7,654,321 double = 11,000.001

Again, the locale of your computer will affect the format. Also, notice that the output for the double is rounded.


QUESTION 4:

Do you suspect that the format method can be used with any numeric type?