本文共 6478 字,大约阅读时间需要 21 分钟。
printf格式化字符串
We’ve already discussed method in a previous tutorial. Today, we’ll discuss the printf() method and its various implementations in detail. Ready. Get. Set. Go!
在上一教程中,我们已经讨论过方法。 今天,我们将详细讨论printf()方法及其各种实现。 准备。 得到。 组。 走!
printf()
方法不仅在C语言中,而且在Java中。 printf()
method is not only there in C, but also in Java.Following are the syntaxes available for the printf()
method:
以下是可用于printf()
方法的语法:
System.out.printf(string);System.out.printf(format, arguments);System.out.printf(locale, format, arguments);
The first one does not do any formatting though and it’s like the println()
method.
第一个虽然没有进行任何格式化,但是它类似于println()
方法。
System.out.format()
is same as System.out.format()
与 System.out.printf()
method. System.out.printf()
方法相同。 String.format()
returns a formatted string. System.out.printf()
also prints a formatted string to the console. String.format()
返回格式化的字符串。 System.out.printf()
也将格式化的字符串打印到控制台。 printf()
uses the java.util.Formatter
class to parse the format string and generate the output. printf()
使用java.util.Formatter
类解析格式字符串并生成输出。 Let’s look at the available format specifiers available for printf
:
让我们看一下可用于printf
的可用格式说明符:
Note: %n or \n are used as line separators in printf()
.
注意 : %n或\ n用作printf()
行分隔符。
Following are the escape characters available in printf()
:
以下是printf()
可用的转义字符:
Let’s look at the full syntax of format specifiers with the extended set:
让我们看一下带有扩展集的格式说明符的完整语法:
%<.precision>specifier
flags can be set as + for right-aligning, and – for left-aligning.
可以将标志设置为+(用于右对齐)和–(用于左对齐)。
Next, fire up your and start using printf()
!
接下来,启动您的并开始使用printf()
!
Here’s an example:
这是一个例子:
| Welcome to JShell -- Version 12.0.1| For an introduction type: /help introjshell> int x = 10x ==> 10jshell> System.out.printf("Formatted output is: %d %d%n", x, -x)Formatted output is: 10 -10
Let’s use some precision formatting:
让我们使用一些精确格式:
jshell> float y = 2.28fy ==> 2.28jshell> System.out.printf("Precision formatting upto 4 decimal places %.4f\n",y)Precision formatting upto 4 decimal places 2.2800jshell> float z = 3.147293165fz ==> 3.147293jshell> System.out.printf("Precision formatting upto 2 decimal places %.2f\n",z)Precision formatting upto 2 decimal places 3.15
As you can see it rounds off to the next decimal in the second case.
如您所见,在第二种情况下,它会四舍五入到下一个小数。
In this section, we’ll see three examples for each of these:
在本节中,我们将为每个示例看到三个示例:
jshell> System.out.printf("'%5.2f'%n", 2.28);' 2.28'
As you can see the width specifier allocates 5 characters width. The content is right aligned by default.
如您所见,宽度说明符分配5个字符的宽度。 默认情况下,内容右对齐。
Filling with zeros
用零填充
Empty spaces to the left of the first character can be filled with zeroes as shown below:
第一个字符左侧的空白可以用零填充,如下所示:
jshell> System.out.printf("'%05.2f'%n", 2.28);'02.28'jshell> System.out.printf("'%010.2f'%n", 2.28);'0000002.28'jshell> System.out.printf("'%010.2f'%n", -2.28);'-000002.28'jshell> System.out.printf("'%010.2f'%n", 1234567.89);'1234567.89'jshell> System.out.printf("'%010.2f'%n", -1234567.89);'-1234567.89'
Aligning
By default, it is a + which means right aligned.对齐
默认情况下,它是一个+,表示右对齐。jshell> System.out.printf("'%10.2f'%n", 2.28);' 2.28'
The following code, aligns to the left:
下面的代码向左对齐:
jshell> System.out.printf("'%-10.2f'%n", 2.28);'2.28 '
Using Comma and Locale:
使用逗号和语言环境:
jshell> System.out.printf(Locale.US, "%,d %n", 5000);5,000
Let’s look at String formatting with a few basic examples:
让我们看一下一些基本示例的字符串格式:
jshell> System.out.printf("%s %s!%n","Hello","World");Hello World!
jshell> System.out.printf("%s\f%s!%n","Hello","World!");Hello World!!
jshell> System.out.printf("%s\\%s!%n","Hello","World!");Hello\World!!
Uppercase:
大写:
jshell> System.out.printf("%s %S!%n","Hello","World");Hello WORLD!
Boolean formatting examples are given below:
布尔格式示例如下:
jshell> System.out.printf("%b%n", false);falsejshell> System.out.printf("%b%n", 0.5);truejshell> System.out.printf("%b%n", "false");true
‘H’, ‘M’, ‘S’
– Hours, Minutes, Seconds
‘L’, ‘N’
– to represent the time in milliseconds and nanoseconds accordingly ‘p’
– AM/PM ‘z’
– prints out the difference from GMT. 'H', 'M', 'S'
–小时,分钟,秒
'L', 'N'
–分别表示毫秒和纳秒的时间 'p'
上午/下午 'z'
打印出与GMT的区别。 jshell> Date date = new Date();date ==> Fri Apr 19 02:15:36 IST 2019jshell> System.out.printf("%tT%n", date);02:15:36jshell> System.out.printf("H : %tH, M: %tM, S: %tS%n",date,date,date)H : 02, M: 15, S: 36
The latter one requires many arguments which are the same.
Instead, we can replace them with a single one:后者需要许多相同的论点。
相反,我们可以将它们替换为一个:jshell> System.out.printf("%1$tH:%1$tM:%1$tS %1$Tp GMT %1$tz %n", date)02:15:36 AM GMT +0530
Date formatting has the following special characters
日期格式具有以下特殊字符
A/a – Full day/Abbreviated day
B/b – Full month/Abbreviated month d – formats a two-digit day of the month m – formats a two-digit month Y – Full year/Last two digits of the Year j – Day of the yearA / a –全天/缩写天
B / b –整月/缩写月 d –格式化一个两位数的日期 m –格式化两位数的月份 Y –全年/年份的最后两位数字 j –一年中的一天jshell> System.out.printf("%s %tB %System.out.printf("%1$td.%1$tm.%1$ty %n", date);19.04.19jshell> System.out.printf("%s %tb %
In this tutorial, we discussed the various types of formatting possible using printf() method.
在本教程中,我们讨论了使用printf()方法可能进行的各种格式化。
翻译自:
printf格式化字符串
转载地址:http://lumzd.baihongyu.com/