Java Tutorial/Development/Formatter Specifiers
Содержание
- 1 %a: Floating-point hexadecimal
- 2 Formatter with different data types
- 3 Formatting Output with Formatter
- 4 Hex: %x, Octal: %o: Integer hexadecimal, Octal integer
- 5 %h: Hash code of the argument
- 6 The Format Specifiers
- 7 The %g format specifier causes Formatter to use either %f or %e, whichever is shorter
- 8 The # symbol shows a digit or nothing if no digit present
- 9 Unknown Format Conversion Exception
- 10 Use Formatter to left-justify strings within a table.
- 11 Use Formatter to vertically align numeric values.
- 12 Using group separators.
- 13 using the %t specifier with Formatter.
%a: Floating-point hexadecimal
<source lang="java">
import java.util.Formatter; public class MainClass {
public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("%a", 123.123); System.out.println(fmt); }
}</source>
0x1.ec7df3b645a1dp6
Formatter with different data types
<source lang="java">
import java.util.Formatter; class FormatDemo {
public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6); System.out.println(fmt); }
}</source>
Formatting Output with Formatter
<source lang="java">
import java.util.Formatter; public class MainClass {
public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6); System.out.println(fmt.toString()); }
}</source>
Formatting with Java is easy 10 98.600000
Hex: %x, Octal: %o: Integer hexadecimal, Octal integer
<source lang="java">
import java.util.Formatter; public class MainClass {
public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("Hex: %x, Octal: %o", 196, 196); System.out.println(fmt); }
}</source>
Hex: c4, Octal: 304 *
%h: Hash code of the argument
<source lang="java">
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("%h", cal); System.out.println(fmt); }
}</source>
75ed2f99
The Format Specifiers
Format SpecifierConversion Applied%a and %AFloating-point hexadecimal%bBoolean%B%cCharacter%C%dDecimal integer%hHash code of the argument%H%eScientific notation%E%fDecimal floating point%gUses %e or %f, whichever is shorter%G%oOctal integer%nInserts a newline character%sString%S%tTime and date%T%xInteger hexadecimal%%Inserts a % sign
The %g format specifier causes Formatter to use either %f or %e, whichever is shorter
<source lang="java">
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>
1000.00 1000.00 100000 1000.00 100000 1.00000e+07 1000.00 100000 1.00000e+07 1.00000e+09
The # symbol shows a digit or nothing if no digit present
<source lang="java">
import java.text.DecimalFormat; import java.text.Format; public class Main {
public static void main(String[] argv) throws Exception { Format formatter = new DecimalFormat("##"); String s = formatter.format(-1234.567); System.out.println(s); s = formatter.format(0); System.out.println(s); }
}</source>
Unknown Format Conversion Exception
<source lang="java">
import java.util.Date; import java.util.Formatter; public class MainClass {
public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("%t", new Date()); System.out.println(fmt); }
}</source>
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = "t" at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2646) at java.util.Formatter.parse(Formatter.java:2477) at java.util.Formatter.format(Formatter.java:2411) at java.util.Formatter.format(Formatter.java:2364) at MainClass.main(MainClass.java:8)
Use Formatter to left-justify strings within a table.
<source lang="java">
import java.util.Formatter; public class Main {
public static void main(String args[]) { Formatter fmt = new Formatter(); fmt.format("%-12s %12s\n\n", "Source", "Loss"); fmt.format("%-12s %,12d\n", "Retail", 1232675); System.out.println(fmt); }
} /* Source Loss Retail 1,232,675
- /</source>
Use Formatter to vertically align numeric values.
<source lang="java">
import java.util.Formatter; public class Main {
public static void main(String[] argv) throws Exception { double data[] = { 12.3, 45.6, -7.89, -1.0, 1.01 }; Formatter fmt = new Formatter(); fmt.format("%12s %12s\n", "Value", "Cube Root"); for (double v : data) { fmt.format("%12.4f %12.4f\n", v, Math.cbrt(v)); } System.out.println(fmt); }
} /* Value Cube Root
12.3000 2.3084 45.6000 3.5726 -7.8900 -1.9908 -1.0000 -1.0000 1.0100 1.0033
- /</source>
Using group separators.
<source lang="java">
import java.util.Formatter; public class Main {
public static void main(String[] argv) throws Exception { Formatter fmt = new Formatter(); fmt.format("No group separators: %d\n", 123456789); fmt.format("With group separators: %,d\n\n", 123456789); System.out.println(fmt); }
} /*No group separators: 123456789 With group separators: 123,456,789
- /</source>
using the %t specifier with Formatter.
<source lang="java">
import java.util.Calendar; import java.util.Formatter; public class Main {
public static void main(String[] argv) throws Exception { Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); // Display 12-hour time format. fmt.format("Time using 12-hour clock: %tr\n", cal); System.out.println(fmt); }
} //Time using 12-hour clock: 03:00:51 PM</source>