Java printf() - 向控制台打印格式化字符串

我们已经在以前的教程中讨论了 Java println() 方法. 今天,我们将详细讨论 printf() 方法及其各种实现。

Java 打印( )

  • printf() 方法不仅在 C 中存在,而且在 Java 中也存在
  • 此方法属于 PrintStream 类
  • 它用于使用各种格式指标来打印格式化字符串

语法

以下是用于printf()方法的语法:

1System.out.printf(string);
2System.out.printf(format, arguments);
3System.out.printf(locale, format, arguments);

虽然第一个不进行任何格式化,但它就像println()方法。

「System.out.format()」與「System.out.printf()」方法相同。

String.format() 和 System.out.printf() 的区别

  1. String.format() 返回一个格式化字符串. System.out.printf() 还将格式化字符串打印到控制台
  2. printf() 使用 java.util.Formatter 类来解析格式字符串并生成输出

格式特征

讓我們來看看「printf」可用的可用格式規格:

  • %c character
  • %d decimal (integer) number (base 10)
  • %e exponential floating-point number
  • %f floating-point number
  • %i integer (base 10)
  • %o octal number (base 8)
  • %s String
  • %u unsigned decimal (integer) number
  • %x number in hexadecimal (base 16)
  • %t formats date/time
  • %% print a percent sign
  • % print a percent sign

** 注: %n\n 被用作 printf() 中的行分离器。

逃跑人物

以下是 printf() 中可用的 escape 字符:

  • \b backspace
  • \f下一行第一个字符开始于当前行最后一个字符
  • \n newline
  • \r carriage return
  • \t tab
  • \ backslash

格式定义 Full Syntax

让我们看看与扩展集合的格式指定符的完整语法:

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

可以将旗帜设置为 + 以便对齐右边,然后 - 以便对齐左边。接下来,点燃您的 Jshell并开始使用 printf()!

数字格式化

这里有一个例子:

1|  Welcome to JShell -- Version 12.0.1
2|  For an introduction type: /help intro
3
4jshell> int x = 10
5x ==> 10
6
7jshell> System.out.printf("Formatted output is: %d %d%n", x, -x)
8Formatted output is: 10 -10

让我们使用一些精确的格式化:

 1jshell> float y = 2.28f
 2y ==> 2.28
 3
 4jshell> System.out.printf("Precision formatting upto 4 decimal places %.4f\n",y)
 5
 6Precision formatting upto 4 decimal places 2.2800
 7
 8jshell> float z = 3.147293165f
 9z ==> 3.147293
10
11jshell> System.out.printf("Precision formatting upto 2 decimal places %.2f\n",z)
12
13Precision formatting upto 2 decimal places 3.15

正如你所看到的,它在第二个案例中圆到下一个十进制。

宽度指标,平衡,用零填充

在本节中,我们将看到三个例子,每一个:

1jshell> System.out.printf("'%5.2f'%n", 2.28);
2' 2.28'

正如您所看到的,宽度指标分配 5 个字符的宽度. 默认情况下,内容是对齐的。 ** 用零填写** 第一个字符的左侧的空白可以用零填写,如下所示:

 1jshell> System.out.printf("'%05.2f'%n", 2.28);
 2'02.28'
 3
 4jshell> System.out.printf("'%010.2f'%n", 2.28);
 5'0000002.28'
 6
 7jshell> System.out.printf("'%010.2f'%n", -2.28);
 8'-000002.28'
 9
10jshell> System.out.printf("'%010.2f'%n", 1234567.89);
11'1234567.89'
12
13jshell> System.out.printf("'%010.2f'%n", -1234567.89);
14'-1234567.89'

** 对齐** 默认情况下,它是 + 意思是右对齐。

1jshell> System.out.printf("'%10.2f'%n", 2.28);
2'      2.28'

以下代码,对齐左边:

1jshell> System.out.printf("'%-10.2f'%n", 2.28);
2'2.28      '

使用Comma和Locale:

1jshell> System.out.printf(Locale.US, "%,d %n", 5000);
25,000

字符串,布尔格式格式

让我们用几个基本例子来看看 String 格式化:

1jshell> System.out.printf("%s %s!%n","Hello","World");
2Hello World!
1jshell> System.out.printf("%s\f%s!%n","Hello","World!");
2Hello
3     World!!
1jshell> System.out.printf("%s\\%s!%n","Hello","World!");
2Hello\World!!

超级:

1jshell> System.out.printf("%s %S!%n","Hello","World");
2Hello WORLD!

下面是 boolean 格式的例子:

1jshell> System.out.printf("%b%n", false);
2false
3
4jshell> System.out.printf("%b%n", 0.5);
5true
6
7jshell> System.out.printf("%b%n", "false");
8true

时间格式化

H, M, S - 小时,分钟,秒L,N - 以毫秒和纳米秒表示时间,相应地p - AM / PMz - 打印了与GMT的差异。

1jshell> Date date = new Date();
2date ==> Fri Apr 19 02:15:36 IST 2019
3
4jshell> System.out.printf("%tT%n", date);
502:15:36
6
7jshell> System.out.printf("H : %tH, M: %tM, S: %tS%n",date,date,date)
8H : 02, M: 15, S: 36

后者需要许多相同的论点,相反,我们可以用一个来代替它们:

1jshell> System.out.printf("%1$tH:%1$tM:%1$tS %1$Tp GMT %1$tz  %n", date)
202:15:36 AM GMT +0530

日期格式化

日期格式有下列特殊字符 A/a - 完整日/缩短日 B/b - 完整月/缩短月 d - 格式一个月的两位数日 m - 格式一个月的两位数月 Y - 完整年 / 最后两位数年 j - 年度日

1jshell> System.out.printf("%s %tB %<te, %<tY", "Current date: ", date);
2Current date:  April 19, 2019
3
4jshell> System.out.printf("%1$td.%1$tm.%1$ty %n", date);
519.04.19
6
7jshell> System.out.printf("%s %tb %<te, %<ty", "Current date: ", date);
8Current date:  Apr 19, 19

结论

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

Published At
Categories with 技术
Tagged with
comments powered by Disqus