printf格式化字符串_Java printf()–将格式化的字符串打印到控制台

printf格式化字符串

We’ve already discussed Java println() method in a previous tutorial. Today, we’ll discuss the printf() method and its various implementations in detail. Ready. Get. Set. Go!

在上一教程中,我们已经讨论过Java println()方法。 今天,我们将详细讨论printf()方法及其各种实现。 准备。 得到。 组。 走!

Java printf()
  • printf()方法不仅在C语言中,而且在Java中。
  • 此方法属于PrintStream类。
  • 它用于使用各种格式说明符打印格式化的字符串。

句法

(Java printf()
  • printf() method is not only there in C, but also in Java.
  • This method belongs to the PrintStream class.
  • It’s used to print formatted strings using various format specifiers.

Syntax

)

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()和System.out.printf()之间的区别 (Difference between String.format() and System.out.printf())

  1. String.format() returns a formatted string. System.out.printf() also prints a formatted string to the console.

    String.format()返回格式化的字符串。 System.out.printf()也将格式化的字符串打印到控制台。
  2. printf() uses the java.util.Formatter class to parse the format string and generate the output.

    printf()使用java.util.Formatter类解析格式字符串并生成输出。

格式说明符 (Format Specifiers)

Let’s look at the available format specifiers available for printf:

让我们看一下可用于printf的可用格式说明符:

  • %c character

    %c个字符
  • %d decimal (integer) number (base 10)

    %d个十进制(整数)数字(以10为底)
  • %e exponential floating-point number

    %e指数浮点数
  • %f floating-point number

    %f个浮点数
  • %i integer (base 10)

    %i整数(以10为底)
  • %o octal number (base 8)

    %o八进制数(以8为底)
  • %s String

    %s字串
  • %u unsigned decimal (integer) number

    %u无符号十进制(整数)数字
  • %x number in hexadecimal (base 16)

    以十六进制表示的%x个数字(以16为基)
  • %t formats date/time

    %t格式化日期/时间
  • %% print a percent sign

    %%打印百分号
  • \% print a percent sign

    \%打印百分号

Note: %n or \n are used as line separators in printf().

注意%n\ n用作printf()行分隔符。

转义字符 (Escape Characters)

Following are the escape characters available in printf():

以下是printf()可用的转义字符:

  • \b backspace

    \ b退格键
  • \f next line first character starts to the right of current line last character

    \ f下一行第一个字符从当前行最后一个字符的右边开始
  • \n newline

    \ n换行符
  • \r carriage return

    \ r回车
  • \t tab

    \ t标签
  • \\ backslash

    \\反斜杠

格式说明符完整语法 (Format Specifiers Full Syntax)

Let’s look at the full syntax of format specifiers with the extended set:

让我们看一下带有扩展集的格式说明符的完整语法:

%<flags><width><.precision>specifier

flags can be set as + for right-aligning, and – for left-aligning.

可以将标志设置为+(用于右对齐)和–(用于左对齐)。

Next, fire up your Jshell and start using printf()!

接下来,启动您的Jshell并开始使用printf()

数字格式 (Number Formatting)

Here’s an example:

这是一个例子:

|  Welcome to JShell -- Version 12.0.1
|  For an introduction type: /help intro

jshell> int x = 10
x ==> 10

jshell> 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.28f
y ==> 2.28

jshell> System.out.printf("Precision formatting upto 4 decimal places %.4f\n",y)

Precision formatting upto 4 decimal places 2.2800

jshell> float z = 3.147293165f
z ==> 3.147293

jshell> 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.

如您所见,在第二种情况下,它会四舍五入到下一个小数。

宽度说明符,对齐,用零填充 (Width Specifier, Aligning, Fill With Zeros)

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

字符串,布尔格式 (String, Boolean formatting)

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);
false

jshell> System.out.printf("%b%n", 0.5);
true

jshell> System.out.printf("%b%n", "false");
true

时间格式 (Time Formatting)

‘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 2019

jshell> System.out.printf("%tT%n", date);
02:15:36

jshell> 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)

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 year

A / a –全天/缩写天
B / b –整月/缩写月
d –格式化一个两位数的日期
m –格式化两位数的月份
Y –全年/年份的最后两位数字
j –一年中的一天

jshell> System.out.printf("%s %tB %<te, %<tY", "Current date: ", date);
Current date:  April 19, 2019

jshell> System.out.printf("%1$td.%1$tm.%1$ty %n", date);
19.04.19

jshell> System.out.printf("%s %tb %<te, %<ty", "Current date: ", date);
Current date:  Apr 19, 19

结论 (Conclusion)

In this tutorial, we discussed the various types of formatting possible using printf() method.

在本教程中,我们讨论了使用printf()方法可能进行的各种格式化。

翻译自: https://www.journaldev.com/28692/java-printf-method

printf格式化字符串

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值