Java by API/java.lang/System.out.printf — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:41, 31 мая 2010
Содержание
- 1 System.out.printf("%03d", int i )
- 2 System.out.printf("%03f", float f)
- 3 System.out.printf("{%07.3f}", float f )
- 4 System.out.printf( "%-s", String word, int length )
- 5 System.out.printf("%1 s...", String str )
- 6 System.out.printf("%1x, %1X", 0xCAFE )
- 7 System.out.printf("%.2f", float f )
- 8 System.out.printf("%5s", String str)
- 9 System.out.printf("%-5s", String str) (2)
- 10 System.out.printf("%.5s", String str) (3)
- 11 System.out.printf("%b", String str )
- 12 System.out.printf("%c", char ch )
- 13 System.out.printf("%e", float )
- 14 System.out.printf("%f", float f )
- 15 System.out.printf("%g", float f )
- 16 System.out.printf("%h", float f )
- 17 System.out.printf( Locale.CHINA, "%tc", Date date )
- 18 System.out.printf( Locale.ITALIAN, "%tc", Date date )
- 19 System.out.printf("%s", 5)
- 20 System.out.printf("%s", Date date )
- 21 System.out.printf("%s://%s/%s\n", String str1, String str2, String str3)
- 22 System.out.printf("%tc", Date date ) (lowercase t, lowercase c)
- 23 System.out.printf("%tC", Date date ) (lowercase t, uppercase C)
- 24 System.out.printf("%Tc", Date date ) (Uppercase T, lowercase c)
- 25 System.out.printf("%tD", Date date )
- 26 System.out.printf("%tF", Date date )
- 27 System.out.printf("%tR",Date date )
- 28 System.out.printf("%tT", Date date )
- 29 System.out.printf("%tz", Date date )
System.out.printf("%03d", int i )
/**
* Output:
num is 005
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("num is %03d\n", 5 );
}
}
System.out.printf("%03f", float f)
/**
* Output:
num is 3.141593
num is {003.140}
06.3 num is 03.142
.3 num is 3.142
9.99 num is 10.00
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("num is %03f\n", 3.14159265 );
System.out.printf("num is {%07.3f}\n", 3.14 );
System.out.printf("06.3 num is %06.3f\n", 3.14159265 );
System.out.printf(".3 num is %.3f\n", 3.14159265 );
System.out.printf("9.99 num is %4.2f\n", 9.999999999 );
}
}
System.out.printf("{%07.3f}", float f )
/**
* Output:
num is 3.141593
num is {003.140}
06.3 num is 03.142
.3 num is 3.142
9.99 num is 10.00
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("num is %03f\n", 3.14159265 );
System.out.printf("num is {%07.3f}\n", 3.14 );
System.out.printf("06.3 num is %06.3f\n", 3.14159265 );
System.out.printf(".3 num is %.3f\n", 3.14159265 );
System.out.printf("9.99 num is %4.2f\n", 9.999999999 );
}
}
System.out.printf( "%-s", String word, int length )
/**
* Output:
Word Length
a 1
aaa 3
aaaaaaaaaa 35
*/
public class MainClass {
public static void main(String args[]) throws Exception {
String [] words =
new String [] { "a", "aaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
System.out.printf( "%-10s %s\n", "Word", "Length" );
for ( String word : words )
System.out.printf( "%-10.10s %s\n", word, word.length() );
}
}
System.out.printf("%1 s...", String str )
/**
Output:
www.jexp.ru...
* */
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("%1$s...", "www.jexp.ru" );
}
}
System.out.printf("%1x, %1X", 0xCAFE )
import java.util.Date;
import java.util.Locale;
/**
* Output:
boolean value is true, TRUE
hex value is 71ee6d51, 71EE6D51
hex value is cafe, CAFE
hex value is cafe, CAFE
hex value is 0xcafe, 0XCAFE
value: 3,140000
*/
public class MainClass {
public static void main(String args[]) throws Exception {
//boolean value is true, TRUE
System.out.printf("boolean value is %1$b, %1$B\n", true );
//hex value is b5151397, B5151397
System.out.printf("hex value is %1$h, %1$H\n", new Date() );
//hex value
System.out.printf("hex value is %1$h, %1$H\n", 0xCAFE );
//hex value
System.out.printf("hex value is %1$x, %1$X\n", 0xCAFE );
System.out.printf("hex value is %1$#x, %1$#X\n", 0xCAFE );
System.out.printf( Locale.ITALIAN, "value: %f\n", 3.14 );
}
}
System.out.printf("%.2f", float f )
/**
* Output:
num is 3.14
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("num is %.2f\n", Math.PI );
}
}
System.out.printf("%5s", String str)
/**
* Output:
String is " A"
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("String is "%5s"\n", "A");
}
}
System.out.printf("%-5s", String str) (2)
/**
* Output:
String is "A "
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("String is "%-5s"\n", "A");
}
}
System.out.printf("%.5s", String str) (3)
/**
* Output:
String is "Happy"
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("String is "%.5s"\n", "Happy Birthday!");
}
}
System.out.printf("%b", String str )
/**
Output:
bool is true
* */
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("bool is %b\n", "a" );
}
}
System.out.printf("%c", char ch )
/**
Output:
char value is a
* */
public class MainClass {
public static void main(String args[]) throws Exception {
//char value is a
System.out.printf("char value is %c\n", "a" );
}
}
System.out.printf("%e", float )
/**
* Output:
sci not num is 3.141593e+04
sci not num is 31415.9
sci not num is 1.234568e-01
sci not num is 0.123457
sci not num is 3.000000e+06
hex num is 3ff00000
num is cafe
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("sci not num is %e\n", 3.14159265*10000 );
System.out.printf("sci not num is %g\n", 3.14159265*10000 );
System.out.printf("sci not num is %e\n", 0.123456789 );
System.out.printf("sci not num is %g\n", 0.123456789 );
System.out.printf("sci not num is %e\n", 3000000.0 );
System.out.printf("hex num is %h\n", 1.0 );
System.out.printf("num is %h\n", 0xCAFE );
}
}
System.out.printf("%f", float f )
/**
* Output:
num is 3.141593
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("num is %f\n", Math.PI );
}
}
System.out.printf("%g", float f )
/**
* Output:
sci not num is 3.141593e+04
sci not num is 31415.9
sci not num is 1.234568e-01
sci not num is 0.123457
sci not num is 3.000000e+06
hex num is 3ff00000
num is cafe
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("sci not num is %e\n", 3.14159265*10000 );
System.out.printf("sci not num is %g\n", 3.14159265*10000 );
System.out.printf("sci not num is %e\n", 0.123456789 );
System.out.printf("sci not num is %g\n", 0.123456789 );
System.out.printf("sci not num is %e\n", 3000000.0 );
System.out.printf("hex num is %h\n", 1.0 );
System.out.printf("num is %h\n", 0xCAFE );
}
}
System.out.printf("%h", float f )
/**
* Output:
sci not num is 3.141593e+04
sci not num is 31415.9
sci not num is 1.234568e-01
sci not num is 0.123457
sci not num is 3.000000e+06
hex num is 3ff00000
num is cafe
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("sci not num is %e\n", 3.14159265*10000 );
System.out.printf("sci not num is %g\n", 3.14159265*10000 );
System.out.printf("sci not num is %e\n", 0.123456789 );
System.out.printf("sci not num is %g\n", 0.123456789 );
System.out.printf("sci not num is %e\n", 3000000.0 );
System.out.printf("hex num is %h\n", 1.0 );
System.out.printf("num is %h\n", 0xCAFE );
}
}
System.out.printf( Locale.CHINA, "%tc", Date date )
import java.util.Date;
import java.util.Locale;
/**
Output:
The date is Fri May 26 11:08:34 PDT 2006
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf( Locale.CHINA, "The date is %tc\n", date );
}
}
System.out.printf( Locale.ITALIAN, "%tc", Date date )
import java.util.Date;
import java.util.Locale;
/**
Output:
The date is ven mag 26 11:07:14 PDT 2006
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf( Locale.ITALIAN, "The date is %tc\n", date );
}
}
System.out.printf("%s", 5)
/**
* Output:
num is 5
char is a
char is A
bool is true
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("num is %s\n", 5);
System.out.printf("char is %s\n", "a");
System.out.printf("char is %S\n", "a");
System.out.printf("bool is %s\n", true );
}
}
System.out.printf("%s", Date date )
import java.util.Date;
/**
Output:
The date is Fri May 26 11:03:46 PDT 2006
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The date is %s\n", date );
}
}
System.out.printf("%s://%s/%s\n", String str1, String str2, String str3)
/**
* Output:
http://host/path
*/
public class MainClass {
public static void main(String args[]) throws Exception {
System.out.printf("%s://%s/%s\n", "http", "host", "path");
}
}
System.out.printf("%tc", Date date ) (lowercase t, lowercase c)
import java.util.Date;
/**
Output:
The date is Fri May 26 11:04:06 PDT 2006
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The date is %tc\n", date );
}
}
System.out.printf("%tC", Date date ) (lowercase t, uppercase C)
import java.util.Date;
/**
Output:
The DATE is 20
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The DATE is %tC\n", date );
}
}
System.out.printf("%Tc", Date date ) (Uppercase T, lowercase c)
import java.util.Date;
/**
Output:
The DATE is FRI MAY 26 11:04:36 PDT 2006
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The DATE is %Tc\n", date );
}
}
System.out.printf("%tD", Date date )
import java.util.Date;
/**
Output:
The DATE is 05/26/06
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The DATE is %tD\n", date );
}
}
System.out.printf("%tF", Date date )
import java.util.Date;
/**
Output:
The DATE is 2006-05-26
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The DATE is %tF\n", date );
}
}
System.out.printf("%tR",Date date )
import java.util.Date;
/**
Output:
The DATE is 11:06
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The DATE is %tR\n", date );
}
}
System.out.printf("%tT", Date date )
import java.util.Date;
/**
Output:
The DATE is 11:06:49
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The DATE is %tT\n", date );
}
}
System.out.printf("%tz", Date date )
import java.util.Date;
/**
Output:
The DATE is -0800
* */
public class MainClass {
public static void main(String args[]) throws Exception {
Date date = new Date();
System.out.printf("The DATE is %tz\n", date );
}
}