value
provides data when format()
creates a string of characters, but value
itself is not changed.
import java.text.*; public class IODemoWrapper { public static void main ( String[] args ) { Integer i = 7654321 ; // create an Integer thru autoboxing Double d = 11000.0008 ; // create a Double thru autoboxing DecimalFormat numform = new DecimalFormat(); System.out.println( "integer = " + numform.format(i) + "\n double = " + numform.format(d) ); } }
A wrapper object may be used as data for format()
.
Recall (from the end of chapter 11)
that a wrapper class defines objects
that hold a primitive value.
An object of class Integer
holds an int
and some methods.
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 affects the format. Also, notice that the output for the double is rounded.
Would the following program fragment work?
Integer a = 123; Integer b = 34; System.out.println( numform.format(a) + " + " + numform.format(b) + " = " + a+b );