/* Written by Jen Chen Updated on June, 2005 Copyrighted by Jen Chen */ import java.io.*; import java.text.*; import java.util.*; public class MyFormat { public static void main(String[] args) throws IOException{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); String[] st = {"Michael Johnson", "Venus Lea", "Maria Fuller", "Kimmy Lou", "Ken Ruby"}; int space = 20; //Give 25 spaces to store a string. System.out.println("Right align:\n"); System.out.println("01234567890123456789012345678901234567890123456789012345678901234567890123456789"); System.out.printf("%30s\n",st[0]); //A POSITIVE value follow the %sign means to align to the RIGHT. System.out.printf("%30s\n",st[1]); System.out.printf("%30s\n",st[2]); System.out.println("Left align:\n"); System.out.println("01234567890123456789012345678901234567890123456789012345678901234567890123456789"); System.out.printf("%-30s\n",st[0]); //Note that we put the - sign in front of the number. The - sign means to align to the LEFT. System.out.printf("%-30s\n",st[1]); System.out.printf("%-30s\n",st[2]); System.out.println(); double[] myD = {0, 1, 3.1415928, 2.7171892}; NumberFormat numF = NumberFormat.getInstance(); DecimalFormat df = new DecimalFormat("####.00"); //numF.setMinimumIntegerDigits(3); numF.setMaximumFractionDigits(3); for(int j = 0; j < myD.length; j++) System.out.print(numF.format(myD[j]) + "\t"); System.out.println(); for(int j = 0; j < myD.length; j++) System.out.print(df.format(myD[j]) + "\t"); System.out.println(); numF.setMaximumFractionDigits(5); for(int j = 0; j < myD.length; j++) System.out.print(numF.format(myD[j]) + "\t"); System.out.println("\n"); NumberFormat dollarF = NumberFormat.getCurrencyInstance(); NumberFormat TaiwanF = NumberFormat.getCurrencyInstance(Locale.TAIWAN); NumberFormat JapanF = NumberFormat.getCurrencyInstance(Locale.JAPANESE); NumberFormat FranceF = NumberFormat.getCurrencyInstance(Locale.FRANCE); NumberFormat UkF = NumberFormat.getCurrencyInstance(Locale.UK); for(int j = 0; j < myD.length; j++){ System.out.print(dollarF.format(myD[j]) + "\t"); System.out.print(TaiwanF.format(myD[j]) + "\t"); System.out.print(JapanF.format(myD[j]) + "\t"); System.out.print(FranceF.format(myD[j])+ "\t"); System.out.println(UkF.format(myD[j])); } } //end of main() static void rightAlign(String str, int howmanyspaces){ for(int i = 0; i < (howmanyspaces - str.length()); i++) System.out.print(' '); //print the number of blank spaces, then the name. Up to //the number of spaces allowed (which is 20) System.out.print(str); //now print the name outside of the inner loop, but inside the outer loop. } //end of method rightAlign() static void leftAlign(String str, int howmanyspaces){ System.out.print(str); //print the name outside of the inner loop, but inside the outer loop. for(int i = 0; i < (howmanyspaces - str.length()); i++) System.out.print(' '); //print the number of blank spaces, then the name. Up to //the number of spaces allowed (which is 20) } //end of method leftAlign() } //end of class MyFormat{}