Java by API/java.util/Formatter
Содержание
- 1 Formatter: format("|%-10.2f|", ) ( left justify )
- 2 Formatter: format("|%10.2f|", ) (Right justify by default)
- 3 Formatter: format("%.15s", String str) (Display at most 15 characters in a string)
- 4 Formatter: format("%16.2e", Format to 2 decimal places in a 16 character field)
- 5 Formatter: format("%4d %4d %4d",
- 6 Formatter: format("%.4f", Format 4 decimal places)
- 7 Formatter: format("% d", -the space format specifiers)
- 8 Formatter: format("|%f|%n|%12f|%n|%012f|", (a field-width specifier)
- 9 Formatter: format("%g", i)
- 10 Formatter: format("%n %d%% ", 88) (the %n and %% format specifiers)
- 11 Formatter: format(String format, Object... args)
- 12 Formatter: format("%tB %tb %tm", cal, cal, cal) (Display month by name and number)
- 13 Formatter: format("%tc", cal) (Display complete time and date information)
- 14 Formatter: format("%te of % < tB, % < tY", cal)
- 15 Formatter: format("%tl:%tM", cal, cal) (Display just hour and minute)
- 16 Formatter: format("%tr", cal) (Display standard 12-hour time format)
- 17 new Formatter(Appendable a)
Formatter: format("|%-10.2f|", ) ( left justify )
/*
|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);
}
}
Formatter: format("|%10.2f|", ) (Right justify by default)
/*
| 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);
}
}
Formatter: format("%.15s", String str) (Display at most 15 characters in a string)
/*
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);
}
}
Formatter: format("%16.2e", Format to 2 decimal places in a 16 character field)
/*
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);
}
}
Formatter: format("%4d %4d %4d",
/*
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);
}
}
Formatter: format("%.4f", Format 4 decimal places)
/*
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);
}
}
Formatter: format("% d", -the space format specifiers)
/*
-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);
}
}
Formatter: format("|%f|%n|%12f|%n|%012f|", (a field-width specifier)
/*
|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);
}
}
Formatter: format("%g", i)
/*
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);
}
}
}
Formatter: format("%n %d%% ", 88) (the %n and %% format specifiers)
/*
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);
}
}
Formatter: format(String format, Object... args)
/*
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);
}
}
}
Formatter: format("%tB %tb %tm", cal, cal, cal) (Display month by name and number)
/*
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);
}
}
Formatter: format("%tc", cal) (Display complete time and date information)
/*
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);
}
}
Formatter: format("%te of % < tB, % < tY", cal)
/*
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);
}
}
Formatter: format("%tl:%tM", cal, cal) (Display just hour and minute)
/*
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);
}
}
Formatter: format("%tr", cal) (Display standard 12-hour time format)
/*
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);
}
}
new Formatter(Appendable a)
/*
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);
}
}