1. บทนำ
ในการกวดวิชานี้เราจะแสดงให้เห็นถึงตัวอย่างที่แตกต่างกันของการจัดรูปแบบกับprintf ()วิธีการ
เมธอดนี้เป็นส่วนหนึ่งของคลาสjava.io.PrintStreamและจัดเตรียมการจัดรูปแบบสตริงที่คล้ายกับฟังก์ชัน printf ()ใน C
2. ไวยากรณ์
เราสามารถใช้วิธีPrintStreamวิธีใดวิธีหนึ่งต่อไปนี้เพื่อจัดรูปแบบผลลัพธ์:
System.out.printf(format, arguments); System.out.printf(locale, format, arguments);
เราระบุกฎการจัดรูปแบบโดยใช้พารามิเตอร์รูปแบบ กฎเริ่มต้นด้วยอักขระ"%"
มาดูตัวอย่างสั้น ๆ ก่อนที่จะเจาะลึกรายละเอียดของกฎการจัดรูปแบบต่างๆ:
System.out.printf("Hello %s!%n", "World");
สิ่งนี้สร้างผลลัพธ์ต่อไปนี้:
Hello World!
ดังที่แสดงไว้ด้านบนสตริงรูปแบบประกอบด้วยข้อความธรรมดาและกฎการจัดรูปแบบสองกฎ กฎข้อแรกใช้เพื่อจัดรูปแบบอาร์กิวเมนต์สตริง กฎข้อที่สองเพิ่มอักขระขึ้นบรรทัดใหม่ที่ส่วนท้ายของสตริง
2.1. กฎรูปแบบ
มาดูสตริงรูปแบบอย่างใกล้ชิดมากขึ้น ประกอบด้วยตัวอักษรและตัวระบุรูปแบบ ตัวระบุรูปแบบประกอบด้วยแฟล็กความกว้างความแม่นยำและอักขระการแปลงตามลำดับต่อไปนี้:
%[flags][width][.precision]conversion-character
ตัวระบุในวงเล็บเป็นทางเลือก
ภายในprintf ()ใช้คลาส java.util.Formatter เพื่อแยกวิเคราะห์สตริงรูปแบบและสร้างเอาต์พุต ตัวเลือกสตริงรูปแบบเพิ่มเติมสามารถพบได้ใน Formatter Javadoc
2.2. อักขระการแปลง
แปลงตัวอักษรที่จำเป็นและกำหนดวิธีการโต้แย้งการจัดรูปแบบ อักขระการแปลงใช้ได้กับข้อมูลบางประเภทเท่านั้น คนทั่วไปบางส่วน ได้แก่ :
- s - จัดรูปแบบสตริง
- d - จัดรูปแบบจำนวนเต็มทศนิยม
- f - จัดรูปแบบตัวเลขทศนิยม
- t - จัดรูปแบบค่าวันที่ / เวลา
เราจะสำรวจสิ่งเหล่านี้และอื่น ๆ อีกสองสามอย่างในบทความนี้
2.3. ตัวปรับแต่งเสริม
[ธง]กำหนดวิธีมาตรฐานในการปรับเปลี่ยนการส่งออกและพบบ่อยที่สุดสำหรับการจัดรูปแบบจำนวนเต็มและตัวเลขทศนิยม
[width]ระบุความกว้างของฟิลด์สำหรับการส่งออกอาร์กิวเมนต์ แสดงจำนวนอักขระขั้นต่ำที่เขียนลงในเอาต์พุต
[.precision]ระบุจำนวนหลักของความแม่นยำเมื่อแสดงค่าทศนิยม นอกจากนี้เรายังสามารถใช้ในการกำหนดความยาวของสตริงย่อยเพื่อสารสกัดจากสตริง
3. เส้นคั่น
ในการแบ่งสตริงออกเป็นบรรทัดแยกกันเรามีตัวระบุ% n :
System.out.printf("baeldung%nline%nterminator");
ข้อมูลโค้ดด้านบนจะสร้างผลลัพธ์ต่อไปนี้:
baeldung line terminator
n%คั่นprintf ()จะแทรกคั่นบรรทัดพื้นเมืองระบบโฮสต์โดยอัตโนมัติ
4. การจัดรูปแบบบูลีน
การจัดรูปแบบค่าบูลีนเราจะใช้b%รูปแบบ มันทำงานได้ด้วยวิธีต่อไปนี้: ถ้าค่าการป้อนข้อมูลที่เป็นจริงผลลัพธ์คือความจริง มิฉะนั้นออกเป็นเท็จ
ดังนั้นถ้าเราทำ:
System.out.printf("%b%n", null); System.out.printf("%B%n", false); System.out.printf("%B%n", 5.3); System.out.printf("%b%n", "random text");
จากนั้นเราจะเห็น:
false FALSE TRUE true
สังเกตว่าเราสามารถใช้% Bสำหรับการจัดรูปแบบตัวพิมพ์ใหญ่
5. การจัดรูปแบบสตริง
การจัดรูปแบบสตริงง่ายที่เราจะใช้% sรวมกัน นอกจากนี้เราสามารถทำให้สตริงเป็นตัวพิมพ์ใหญ่:
printf("'%s' %n", "baeldung"); printf("'%S' %n", "baeldung");
และผลลัพธ์คือ:
'baeldung' 'BAELDUNG'
นอกจากนี้ในการระบุความยาวขั้นต่ำเราสามารถระบุความกว้าง :
printf("'%15s' %n", "baeldung");
ซึ่งทำให้เรา:
' baeldung'
หากเราต้องการจัดเรียงสตริงของเราทางซ้ายเราสามารถใช้แฟล็ก '-' :
printf("'%-10s' %n", "baeldung");
และผลลัพธ์คือ:
'baeldung '
Even more, we can limit the number of characters in our output by specifying a precision:
System.out.printf("%2.2s", "Hi there!");
The first ‘x' number in %x.ys syntax is the padding. ‘y' is the number of chars.
For our example here, the output is Hi.
6. Char Formatting
The result of %c is a Unicode character:
System.out.printf("%c%n", 's'); System.out.printf("%C%n", 's');
The capital letter C will uppercase the result:
s S
But, if we give it an invalid argument, then Formatter will throw IllegalFormatConversionException.
7. Number Formatting
7.1. Integer Formatting
The printf() method accepts all the integers available in the language; byte, short, int, long and BigInteger if we use %d:
System.out.printf("simple integer: %d%n", 10000L);
With the help of the ‘d' character, we'll have:
simple integer: 10000
In case we need to format our number with the thousands separator, we can use the ‘,'flag. And we can also format our results for different locales:
System.out.printf(Locale.US, "%,d %n", 10000); System.out.printf(Locale.ITALY, "%,d %n", 10000);
As we see, the formatting in the US is different than in Italy:
10,000 10.000
7.2. Float and Double Formatting
To format a float number, we'll need the ‘f' format:
System.out.printf("%f%n", 5.1473);
Which will output:
5.147300
Of course, the first thing that comes to mind is to control the precision:
System.out.printf("'%5.2f'%n", 5.1473);
Here we define the width of our number as 5, and the length of the decimal part is 2:
' 5.15'
Here we have one space padding from the beginning of the number to support the predefined width.
To have our output in scientific notation, we just use the ‘e' conversion character:
System.out.printf("'%5.2e'%n", 5.1473);
And the result is the following:
'5.15e+00'
8. Date and Time Formatting
For date and time formatting, the conversion string is a sequence of two characters: the ‘t' or ‘T' character and the conversion suffix. Let's explore the most common time and date formatting suffix characters with the examples.
Definitely, for more advanced formatting we can use DateTimeFormatter which has been available since Java 8.
8.1. Time Formatting
First, let's see the list of some useful suffix characters for Time Formatting:
- ‘H', ‘M', ‘S' – characters are responsible for extracting the hours, minutes and second from the input Date
- ‘L', ‘N' – to represent the time in milliseconds and nanoseconds accordingly
- ‘p' – adds am/pm formatting
- ‘z' – prints out the timezone offset
Now, let's say we wanted to print out the time part of a Date:
Date date = new Date(); System.out.printf("%tT%n", date);
The code above along with ‘%tT' combination produces the following output:
13:51:15
In case we need more detailed formatting, we can call for different time segments:
System.out.printf("hours %tH: minutes %tM: seconds %tS%n", date, date, date);
Having used ‘H', ‘M', and ‘S' we get:
hours 13: minutes 51: seconds 15
Though, listing date multiple times is a pain. Alternatively, to get rid of multiple arguments, we can use the index reference of our input parameter which is 1$ in our case:
System.out.printf("%1$tH:%1$tM:%1$tS %1$tp %1$tL %1$tN %1$tz %n", date);
Here we want as an output the current time, am/pm, time in milliseconds, nanoseconds and the timezone offset:
13:51:15 pm 061 061000000 +0400
8.2. Date Formatting
Like time formatting, we have special formatting characters for date formatting:
- ‘A' – prints out the full day of the week
- ‘d' – formats a two-digit day of the month
- ‘B' – is for the full month name
- ‘m' – formats a two-digit month
- ‘Y' – outputs a year in four digits
- ‘y' – outputs the last two digits of the year
So, if we wanted to show the day of the week, followed by the month:
System.out.printf("%1$tA, %1$tB %1$tY %n", date);
Then using ‘A', ‘B', and ‘Y', we'd get:
Thursday, November 2018
To have our results all in numeric format, we can replace the ‘A', ‘B', ‘Y ‘ letters with ‘d', ‘m', ‘y':
System.out.printf("%1$td.%1$tm.%1$ty %n", date);
Which will result in:
22.11.18
9. Summary
ในบทความนี้เราได้กล่าวถึงวิธีการใช้เมธอด PrintStream # printfเพื่อจัดรูปแบบผลลัพธ์ เราดูรูปแบบรูปแบบต่างๆที่ใช้ในการควบคุมเอาต์พุตสำหรับชนิดข้อมูลทั่วไป
สุดท้ายเช่นเคยรหัสที่ใช้ในการสนทนาสามารถพบได้บน GitHub