// Java0605.java // This program demonstrates how to "right justify" integers with an object // of the <DecimalFormat> class and the <format> method. import java.text.DecimalFormat; // necessary to use DecimalFormat public class Java0605 { public static void main (String args[]) { System.out.println("\nJAVA0605.JAVA\n"); DecimalFormat output = new DecimalFormat("00000"); System.out.println(output.format(1)); System.out.println(output.format(12)); System.out.println(output.format(123)); System.out.println(output.format(1234)); System.out.println(output.format(12345)); System.out.println(output.format(123456)); System.out.println(output.format(1234567)); System.out.println(); } } Figure 6.10 Continued Program Java0606.java, in Figure 6.11, demonstrates that it is possible to display large numbers with the traditional commas displayed every third digit. Figure 6.11 // Java0606.java // This program demonstrates how to insert commas in numerical output // with a <DecimalFormat> object. import java.text.DecimalFormat; public class Java0606 { public static void main (String args[]) { System.out.println("\nJAVA0606.JAVA\n"); DecimalFormat output = new DecimalFormat("0,000,000"); System.out.println(output.format(1));
// Java0605.java // This program demonstrates how to "right justify" integers with an object // of the <DecimalFormat> class and the <format> method. import java.text.DecimalFormat; // necessary to use DecimalFormat public class Java0605 { public static void main (String args[]) { System.out.println("\nJAVA0605.JAVA\n"); DecimalFormat output = new DecimalFormat("00000"); System.out.println(output.format(1)); System.out.println(output.format(12)); System.out.println(output.format(123)); System.out.println(output.format(1234)); System.out.println(output.format(12345)); System.out.println(output.format(123456)); System.out.println(output.format(1234567)); System.out.println(); } } Figure 6.10 Continued Program Java0606.java, in Figure 6.11, demonstrates that it is possible to display large numbers with the traditional commas displayed every third digit. Figure 6.11 // Java0606.java // This program demonstrates how to insert commas in numerical output // with a <DecimalFormat> object. import java.text.DecimalFormat; public class Java0606 { public static void main (String args[]) { System.out.println("\nJAVA0606.JAVA\n"); DecimalFormat output = new DecimalFormat("0,000,000"); System.out.println(output.format(1));
Text06-ClassAndObjectMethods.docx