Java by API/java.util/Formatter

Материал из Java эксперт
Перейти к: навигация, поиск

Formatter: format("|%-10.2f|", ) ( left justify )

   <source lang="java">

/* |123.12 |

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   // left justify. 
   fmt.format("|%-10.2f|", 123.123); 
   System.out.println(fmt); 
 }

}

      </source>
   
  
 
  



Formatter: format("|%10.2f|", ) (Right justify by default)

   <source lang="java">

/* | 123.12|

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   // Right justify by default 
   fmt.format("|%10.2f|", 123.123); 
   System.out.println(fmt); 
 }

}

      </source>
   
  
 
  



Formatter: format("%.15s", String str) (Display at most 15 characters in a string)

   <source lang="java">

/*

Formatting with
*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   // Display at most 15 characters in a string.
   fmt = new Formatter();
   fmt.format("%.15s", "Formatting with Java is now easy.");
   System.out.println(fmt);
 }

}

      </source>
   
  
 
  



Formatter: format("%16.2e", Format to 2 decimal places in a 16 character field)

   <source lang="java">

/*

       1.23e+02
*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   // Format to 2 decimal places in a 16 character field. 
   fmt = new Formatter(); 
   fmt.format("%16.2e", 123.1234567); 
   System.out.println(fmt); 
 }

}

      </source>
   
  
 
  



Formatter: format("%4d %4d %4d",

   <source lang="java">

/*

 1    2    3
*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   
   fmt.format("%4d %4d %4d", 1, 2, 3); 
   System.out.println(fmt); 
 }

}

      </source>
   
  
 
  



Formatter: format("%.4f", Format 4 decimal places)

   <source lang="java">

/* 123.1235

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   // Format 4 decimal places. 
   fmt.format("%.4f", 123.1234567); 
   System.out.println(fmt); 
 }

}

      </source>
   
  
 
  



Formatter: format("% d", -the space format specifiers)

   <source lang="java">

/* -100

100

-200

200
*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   fmt.format("% d", -100); 
   System.out.println(fmt); 

   fmt = new Formatter(); 
   fmt.format("% d", 100); 
   System.out.println(fmt); 

   fmt = new Formatter(); 
   fmt.format("% d", -200); 
   System.out.println(fmt); 

   fmt = new Formatter(); 
   fmt.format("% d", 200); 
   System.out.println(fmt); 
 }

}

      </source>
   
  
 
  



Formatter: format("|%f|%n|%12f|%n|%012f|", (a field-width specifier)

   <source lang="java">

/* |10.123450| | 10.123450| |00010.123450|

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   fmt.format("|%f|%n|%12f|%n|%012f|", 10.12345, 10.12345, 10.12345);
   System.out.println(fmt);
 }

}

      </source>
   
  
 
  



Formatter: format("%g", i)

   <source lang="java">

/* 1000.00 1000.00 100000 1000.00 100000 1.00000e+07 1000.00 100000 1.00000e+07 1.00000e+09

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   for (double i = 1000; i < 1.0e+10; i *= 100) {
     fmt.format("%g ", i);
     System.out.println(fmt);
   }
 }

}

      </source>
   
  
 
  



Formatter: format("%n %d%% ", 88) (the %n and %% format specifiers)

   <source lang="java">

/* Copying file Transfer is 88% complete

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   fmt.format("Copying file%nTransfer is %d%% complete", 88);
   System.out.println(fmt);
 }

}

      </source>
   
  
 
  



Formatter: format(String format, Object... args)

   <source lang="java">

/* 1000.00 1000.00 100000 1000.00 100000 1.00000e+07 1000.00 100000 1.00000e+07 1.00000e+09

*/

import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   for (double i = 1000; i < 1.0e+10; i *= 100) {
     fmt.format("%g ", i);
     System.out.println(fmt);
   }
 }

}

      </source>
   
  
 
  



Formatter: format("%tB %tb %tm", cal, cal, cal) (Display month by name and number)

   <source lang="java">

/*

June Jun 06
*/

import java.util.Calendar; import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   // Display month by name and number. 
   fmt = new Formatter(); 
   fmt.format("%tB %tb %tm", cal, cal, cal); 
   System.out.println(fmt); 
 }

}

      </source>
   
  
 
  



Formatter: format("%tc", cal) (Display complete time and date information)

   <source lang="java">

/* Wed Jun 07 15:18:00 PDT 2006

*/

import java.util.Calendar; import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt = new Formatter();
   fmt.format("%tc", cal);
   System.out.println(fmt);
 }

}

      </source>
   
  
 
  



Formatter: format("%te of % < tB, % < tY", cal)

   <source lang="java">

/* Today is day 7 of June, 2006

*/

import java.util.Calendar; import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt.format("Today is day %te of %<tB, %<tY", cal);
   System.out.println(fmt);
 }

}

      </source>
   
  
 
  



Formatter: format("%tl:%tM", cal, cal) (Display just hour and minute)

   <source lang="java">

/*

4:44
*/

import java.util.Calendar; import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   // Display just hour and minute. 
   fmt = new Formatter(); 
   fmt.format("%tl:%tM", cal, cal); 
   System.out.println(fmt); 
 }

}

      </source>
   
  
 
  



Formatter: format("%tr", cal) (Display standard 12-hour time format)

   <source lang="java">

/*

1000.00 
1000.00 100000 
1000.00 100000 1.00000e+07 
1000.00 100000 1.00000e+07 1.00000e+09 
*/

import java.util.Calendar; import java.util.Formatter; public class MainClass {

 public static void main(String args[]) {
   Formatter fmt = new Formatter();
   Calendar cal = Calendar.getInstance();
   fmt.format("%tr", cal);
   System.out.println(fmt);
 }

}

      </source>
   
  
 
  



new Formatter(Appendable a)

   <source lang="java">

/*

  2)+1234.12
*/

import java.util.Formatter; public class MainClass {

 public static void main(String[] args) throws Exception {
   StringBuilder buf = new StringBuilder(); // Buffer to hold output
   Formatter formatter = new Formatter(buf); // Formatter to format data into
                                             // buf
   formatter.format("%4d)%+7.2f", 2, 1234.1234);
   System.out.println(buf);
 }

}

      </source>