Java/Development Class/printf
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Align float numbers
- 2 Align string output
- 3 Basic Math with System.out.printf
- 4 "%d %(d %+d %05d
- 5 Default floating-point format: %f
- 6 Display a file in hex.
- 7 Display current time
- 8 Display short and long date and time formats.
- 9 Floating-point with commas: %,f
- 10 give a known Locale (with the right decimal separator) to bypass the current Locale
- 11 Group separators
- 12 Line up positive and negative values
- 13 Negative floating-point default: %,f
- 14 Negative floating-point option: %,(f
- 15 System.out.format("%f, %1$+020.10f %n", Math.PI)
- 16 System.out.printf( "%-10.10s %s\n", word, word.length() )
- 17 System.out.printf("1.0 4f num is %4f\n", 1.0 )
- 18 System.out.printf("%1$s://%2$s/%3$s\n", "http", "host", "path")
- 19 System.out.printf("a %1$s is a %1$s is a %1$s...", "AAA")
- 20 System.out.printf("%b %n %c %n %s %n %s %n %d %n",..)
- 21 System.out.printf("boolean value is %1$b, %1$B\n", true )
- 22 System.out.printf("float is %-20f!\n", 1.23456789)
- 23 System.out.printf("hex value is %1$#x, %1$#X\n", 0xCAFE )
- 24 System.out.printf(Locale.CHINA, "The date is %tc\n", new Date())
- 25 System.out.printf( Locale.ITALIAN, "value: %f\n", 3.14 )
- 26 System.out.printf("num is %03d\n", 5 )
- 27 System.out.printf("num is {%07.3f}\n", 3.14 )
- 28 System.out.printf("num is %.2f\n", Math.PI )
- 29 System.out.printf("num is %s\n", 5)
- 30 System.out.printf("sci not num is %e\n", 3000000.0 )
- 31 System.out.printf("%s://%s/%s\n", "http", "host", "path")
- 32 System.out.printf("String is "%-5s"\n", "A")
- 33 System.out.printf("The DATE is %Tc\n", new Date() )
- 34 System.out.printf("The DATE is %tr\n", new Date() )
- 35 System.out.printf("The DATE is %tz\n", new Date() )
- 36 Two decimal digits
- 37 Use printf(): %10s %10s %10s
- 38 Use printf(): align float numbers
- 39 Use printf(): group separators
- 40 Use printf() to create a time stamp.
- 41 Use printf(): Two decimal digits
Align float numbers
public class Main {
public static void main(String[] argv) throws Exception {
for (double i = 1.0; i < 20.0; i++)
System.out.printf("%10.2f %10.2f %10.2f\n", i, Math.sqrt(i), i * i);
}
}
/* 1.00 1.00 1.00
2.00 1.41 4.00
3.00 1.73 9.00
4.00 2.00 16.00
5.00 2.24 25.00
6.00 2.45 36.00
7.00 2.65 49.00
8.00 2.83 64.00
9.00 3.00 81.00
10.00 3.16 100.00
11.00 3.32 121.00
12.00 3.46 144.00
13.00 3.61 169.00
14.00 3.74 196.00
15.00 3.87 225.00
16.00 4.00 256.00
17.00 4.12 289.00
18.00 4.24 324.00
19.00 4.36 361.00
*/
Align string output
public class Main {
public static void main(String[] argv) throws Exception {
System.out.printf("%10s %10s %10s\n", "this", "is", "a test");
}
}
Basic Math with System.out.printf
/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public class BasicMathDemo {
public static void main(String[] args) {
double a = -191.635;
double b = 43.74;
int c = 16, d = 45;
System.out.printf("The absolute value of %.3f is %.3f%n", a, Math.abs(a));
System.out.printf("The ceiling of %.2f is %.0f%n", b, Math.ceil(b));
System.out.printf("The floor of %.2f is %.0f%n", b, Math.floor(b));
System.out.printf("The rint of %.2f is %.0f%n", b, Math.rint(b));
System.out.printf("The max of %d and %d is %d%n", c, d, Math.max(c, d));
System.out.printf("The min of of %d and %d is %d%n", c, d, Math.min(c, d));
}
}
"%d %(d %+d %05d
public class MainClass{
public static void main(String args[]) {
System.out.printf("%d %(d %+d %05d\n", 3, -3, 3, 3);
}
}
//3 (3) +3 00003
Default floating-point format: %f
public class MainClass{
public static void main(String args[]) {
System.out.printf("Default floating-point format: %f\n", 1234567.123);
}
}
//Default floating-point format: 1234567.123000
Display a file in hex.
import java.io.FileInputStream;
public class Main {
public static void main(String[] argv) throws Exception {
FileInputStream fin = new FileInputStream("test.txt");
int i;
do {
i = fin.read();
if (i != -1)
System.out.printf("%02X ", i);
} while (i != -1);
fin.close();
}
}
Display current time
import java.util.Calendar;
public class Main {
public static void main(String[] argv) throws Exception {
Calendar cal = Calendar.getInstance();
System.out.printf("Current time and date: %tc\n", cal);
}
}
Display short and long date and time formats.
import java.text.DateFormat;
import java.util.Date;
class DateFormatDemo {
public static void main(String args[]) {
Date date = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println("Short form: " + df.format(date));
}
}
Floating-point with commas: %,f
public class MainClass{
public static void main(String args[]) {
System.out.printf("Floating-point with commas: %,f\n", 1234567.123);
}
}
//Floating-point with commas: 1,234,567.123000
give a known Locale (with the right decimal separator) to bypass the current Locale
import java.util.Locale;
public class Main {
public static void main(String[] argv) {
System.out.printf(Locale.UK, "%6.2f%n", 123456.7890);
}
}
//123456.79
Group separators
public class Main {
public static void main(String[] argv) throws Exception {
System.out.printf("group separators: %,.2f\n\n", 1546456.87);
}
}
Line up positive and negative values
public class MainClass{
public static void main(String args[]) {
System.out.printf("Line up positive and negative values:\n");
System.out.printf("% ,.2f\n% ,.2f\n", 1234567.123, -1234567.123);
}
}
//Line up positive and negative values:
// 1,234,567.12
//-1,234,567.12
Negative floating-point default: %,f
public class MainClass{
public static void main(String args[]) {
System.out.printf("Negative floating-point default: %,f\n", -1234567.123);
}
}
//Negative floating-point default: -1,234,567.123000
Negative floating-point option: %,(f
public class MainClass{
public static void main(String args[]) {
System.out.printf("Negative floating-point option: %,(f\n", -1234567.123);
}
}
//Negative floating-point option: (1,234,567.123000)
System.out.format("%f, %1$+020.10f %n", Math.PI)
/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public class Format {
public static void main(String[] args) {
System.out.format("%f, %1$+020.10f %n", Math.PI);
}
}
//3.141593, +00000003.1415926536
System.out.printf( "%-10.10s %s\n", word, word.length() )
public class PrintfExamples {
public static void main(String[] args) {
String [] words = new String [] { "a", "ape", "asdfasdfasdfasdfasdfasdfasdfadsfasdf" };
System.out.printf( "%-10s %s\n", "Word", "Length" );
for ( String word : words )
System.out.printf( "%-10.10s %s\n", word, word.length() );
}
}
/*
Word Length
a 1
ape 3
asdfasdfas 36
*/
System.out.printf("1.0 4f num is %4f\n", 1.0 )
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf("1.0 5f num is %5f\n", 1.0 );
System.out.printf("1.0 4f num is %4f\n", 1.0 );
}
}
/*
1.0 5f num is 1.000000
1.0 4f num is 1.000000
*/
System.out.printf("%1$s://%2$s/%3$s\n", "http", "host", "path")
public class PrintfExamples {
public static void main(String[] args) {
//http://host/path
System.out.printf("%1$s://%2$s/%3$s\n", "http", "host", "path");
}
}
/*
http://host/path
*/
System.out.printf("a %1$s is a %1$s is a %1$s...", "AAA")
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf("a %1$s is a %1$s is a %1$s...", "AAA");
System.out.printf("bool is %b\n", "a");
// char value is a
System.out.printf("char value is %c\n", "a");
}
}
/*a AAA is a AAA is a AAA...bool is true
char value is a
*/
System.out.printf("%b %n %c %n %s %n %s %n %d %n",..)
/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.IOException;
public class PrintfStuff {
public static void main(String[] args) throws IOException {
char three[] = { "t", "h", "r", "e", "e" };
System.out.printf("%b %n %c %n %s %n %s %n %d %n"
+ "%d %n %g %n %g %n %s %n", !false, "3", new String(three), "Three",
3, Long.MAX_VALUE, Math.PI, Double.MAX_VALUE, new Object());
}
}
/*true
3
three
Three
3
9223372036854775807
3.14159
1.79769e+308
java.lang.Object@61de33
*/
System.out.printf("boolean value is %1$b, %1$B\n", true )
public class PrintfExamples {
public static void main(String[] args) {
//boolean value is true, TRUE
System.out.printf("boolean value is %1$b, %1$B\n", true );
}
}
//boolean value is true, TRUE
System.out.printf("float is %-20f!\n", 1.23456789)
import java.util.Locale;
public class PrintfExamples {
public static void main(String[] args) {
// flags
System.out.printf("num is %d\n", 5000000);
System.out.printf("float is %f\n", 1.23456789);
System.out.printf("float is %.3f\n", 1.23456789);
System.out.printf("float is %.1f\n", 1.23456789);
System.out.printf("float is %.0f\n", 1.23456789);
System.out.printf("float is %-20f!\n", 1.23456789);
}
}
/*num is 5000000
float is 1.234568
float is 1.235
float is 1.2
float is 1
float is 1.234568 */
System.out.printf("hex value is %1$#x, %1$#X\n", 0xCAFE )
import java.util.Date;
public class PrintfExamples {
public static void main(String[] args) {
//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 );
}
}
/*hex value is 5a341926, 5A341926
hex value is cafe, CAFE
hex value is cafe, CAFE
hex value is 0xcafe, 0XCAFE*/
System.out.printf(Locale.CHINA, "The date is %tc\n", new Date())
import java.util.Date;
import java.util.Locale;
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf(Locale.ITALIAN, "The date is %tc\n", new Date());
System.out.printf(Locale.CHINA, "The date is %tc\n", new Date());
System.out.printf(Locale.FRENCH, "The date is %tc\n", new Date());
System.out.printf(Locale.GERMAN, "The date is %tc\n", new Date());
}
}
/*
The date is ven ott 31 16:28:06 PDT 2008
The date is ??? ?? 31 16:28:07 PDT 2008
The date is ven. oct. 31 16:28:08 PDT 2008
The date is Fr Okt 31 16:28:08 PDT 2008
*/
System.out.printf( Locale.ITALIAN, "value: %f\n", 3.14 )
import java.util.Locale;
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf( Locale.ITALIAN, "value: %f\n", 3.14 );
}
}
/*value: 3,140000*/
System.out.printf("num is %03d\n", 5 )
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf("num is %03d\n", 5 );
}
}
/*
num is 005
*/
System.out.printf("num is {%07.3f}\n", 3.14 )
public class PrintfExamples {
public static void main(String[] args) {
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 );
}
}
/*
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
*/
System.out.printf("num is %.2f\n", Math.PI )
public class PrintfExamples {
public static void main(String[] args) {
// num is 3.141593
System.out.printf("num is %f\n", Math.PI );
// num is 3.14
System.out.printf("num is %.2f\n", Math.PI );
}
}
/*
num is 3.141593
num is 3.14
*/
System.out.printf("num is %s\n", 5)
public class PrintfExamples {
public static void main(String[] args) {
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 );
}
}
/*
num is 5
char is a
char is A
bool is true
*/
System.out.printf("sci not num is %e\n", 3000000.0 )
public class PrintfExamples {
public static void main(String[] args) {
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 );
}
}
/*
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
*/
System.out.printf("%s://%s/%s\n", "http", "host", "path")
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf("My name is %s\n", "Joe");
System.out.printf("%s://%s/%s\n", "http", "host", "path");
}
}
/*
My name is Joe
http://host/path
*/
System.out.printf("String is "%-5s"\n", "A")
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf("String is "%5s"\n", "A");
System.out.printf("String is "%-5s"\n", "A");
System.out.printf("String is "%.5s"\n", "Happy Birthday!");
}
}
/*
String is " A"
String is "A "
String is "Happy"
*/
System.out.printf("The DATE is %Tc\n", new Date() )
import java.util.Date;
import java.util.Locale;
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf("The date is %s\n", new Date() );
System.out.printf("The date is %tc\n", new Date() );
System.out.printf("The DATE is %Tc\n", new Date() );
}
}
/*
The date is Fri Oct 31 16:30:25 PDT 2008
The date is Fri Oct 31 16:30:25 PDT 2008
The DATE is FRI OCT 31 16:30:25 PDT 2008
*/
System.out.printf("The DATE is %tr\n", new Date() )
import java.util.Date;
import java.util.Locale;
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf("The DATE is %tD\n", new Date() );
System.out.printf("The DATE is %tF\n", new Date() );
System.out.printf("The DATE is %tr\n", new Date() );
System.out.printf("The DATE is %tR\n", new Date());
System.out.printf("The DATE is %tT\n", new Date());
}
}
/*
The DATE is 10/31/08
The DATE is 2008-10-31
The DATE is 04:29:46 PM
The DATE is 16:29
The DATE is 16:29:46
*/
System.out.printf("The DATE is %tz\n", new Date() )
import java.util.Date;
public class PrintfExamples {
public static void main(String[] args) {
System.out.printf("The DATE is %tC\n", new Date() );
System.out.printf("The DATE is %tz\n", new Date());
}
}
/*
The DATE is 20
The DATE is -0800
*/
Two decimal digits
public class Main {
public static void main(String[] argv) throws Exception {
System.out.printf("Two decimal digits: %.2f\n", 10.0 / 3.0);
}
}
Use printf(): %10s %10s %10s
public class Main {
public static void main(String[] argv) throws Exception {
System.out.printf("%10s %10s %10s\n", "this", "is", "a test");
}
}
// this is a test
Use printf(): align float numbers
public class Main {
public static void main(String[] argv) throws Exception {
for (double i = 1.0; i < 20.0; i++)
System.out.printf("%10.2f %10.2f %10.2f\n", i, Math.sqrt(i), i * i);
}
}
/* 1.00 1.00 1.00
2.00 1.41 4.00
3.00 1.73 9.00
4.00 2.00 16.00
5.00 2.24 25.00
6.00 2.45 36.00
7.00 2.65 49.00
8.00 2.83 64.00
9.00 3.00 81.00
10.00 3.16 100.00
11.00 3.32 121.00
12.00 3.46 144.00
13.00 3.61 169.00
14.00 3.74 196.00
15.00 3.87 225.00
16.00 4.00 256.00
17.00 4.12 289.00
18.00 4.24 324.00
19.00 4.36 361.00
*/
Use printf(): group separators
public class Main {
public static void main(String[] argv) throws Exception {
System.out.printf("Use group separators: %,.2f\n\n", 123456.87);
}
}
//Use group separators: 123,456.87
Use printf() to create a time stamp.
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Calendar;
public class Main {
static void timeStamp(String msg, PrintWriter pw) {
Calendar cal = Calendar.getInstance();
pw.printf("%s %tc\n", msg, cal);
}
public static void main(String args[]) throws Exception {
PrintWriter pw = new PrintWriter(new FileWriter("logfile.txt", true));
timeStamp("File opened", pw);
pw.close();
if (pw.checkError())
System.out.println("I/O error occurred.");
}
}
Use printf(): Two decimal digits
public class Main {
public static void main(String[] argv) throws Exception {
System.out.printf("Two decimal digits: %.2f\n", 10.0 / 3.0);
}
}
//Two decimal digits: 3.33