java 中的格式化程序

Formatters in java

%b,%c,%d,%f,%s 这在 java 中如何运作?我一直在尝试阅读 Formatter class 和 formattable interface 但是,我无法理解作为参数传递的转换。

例如:

System.out.printf("%f not equals %b", Math.PI, Math.E)

虽然ocjp6考试中%b, %c, %d, %f, %s之类的formatter是有限的,但感觉准备的题目很大

%b is for Boolean
%f is for Decimal floating-point
%c is for Character
%d is for Decimal integer
%s is for String

你可以查看这个 reference. Also the Oracle docs 详细解释。

我认为您无法理解 System.out.printf() 的工作原理。一旦你明白了,这很简单。

你原来的问题是关于下面的

System.out.printf("%f not equals %b", Math.PI, Math.E)

这里 System.out.printf 试图输出一个 String 。 %f 和 %b 可以理解为有特殊含义的占位符

占位符,因为它们将被逗号后的数据替换。在这种情况下,%f 被替换为 Math.PI 的值,%b 被替换为 Math.E

的值

特殊含义,因为每个格式化程序都代表某些东西,例如上面提到的

%b is for Boolean
%f is for Decimal floating-point
%c is for Character
%d is for Decimal integer
%s is for String

现在以简单的方式为您编写原始查询

System.out.printf("%f is not equals to %b", 3.14, true);

这里%f(意思是十进制浮点数)被替换为浮点值3.14,%b被替换为值"true".

如果你在上面切换 %f 和 %b 就像

System.out.printf("%b is not equal to %f", 3.14, true); //error
 because "true" (boolean)value is not compatible with %f

但这行得通

System.out.printf("%b is not equal to %f", 3.14, 3.14); // 会起作用,因为 3.14 的计算结果为真 由于 java 进行了一些自动类型转换,所以上面的代码有效。不过你可以稍后再看。

现在关于你的最后一个问题

System.out.println("%+04.2f",12.6542); ?

我猜你指的是 printf 。

现在所有格式化程序及其解释都出现在 link

http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

它可能看起来令人生畏。但这很容易。

Lets figure out what %+04.2f stands for from the above link 

'+' Requires the output to include a positive sign for all positive numbers. 
    If this flag is not given  only negative values will include sign.

'0' Requires the output to be padded with leading zeros to the minimum field width following any sign. Called zero padding  

4.2 indicates that a floating point number is displayed in a total of 4 character spaces, including 2 digits after the decimal.
    that means 22.5555 will be shown as 22.55(total 4 char and two after space) 
    Read Width and precision in the link given above.

f   floating point  The result is formatted as a decimal number

所以基本上 %+04.2f 的意思是对所有正数显示一个正号 numbers.The 总共应该有 4 个字符,小数点后有两个字符。它应该被格式化为浮点数。

更多示例

       System.out.printf("  %04.2f",12.6542);   output ==> 12.65
       System.out.printf("  %+04.2f",12.6542);  output ==> +12.65(plus sign here bcoz we gave +)
       System.out.printf("  %+04.2f",-12.6542); output ==> -12.65

       System.out.printf("  %02d",1);   output ==> 1 
       System.out.printf("  %02d",1);   output ==> 01 (bcoz of 02d) 
       System.out.printf("  %03d",1);   output ==> 001 (bcoz of 03d)


       System.out.printf("  %+04.2f",22.2);     output ==> +22.20
       System.out.printf("  %+04.2f",2222.125); output ==> +2222.13 
       (left side of decimal is never truncated . so all chars shows ie total 6 chars even though only 4 asked 

       System.out.printf("  %+04.0f",2222.125); output ==> +2222 (bcoz zero chars requested after decimal point) 

请阅读下面的 links 。它将帮助您更轻松地理解这个概念

http://www.homeandlearn.co.uk/java/java_formatted_strings.html

https://answers.yahoo.com/question/index?qid=20101017181211AAbtWC0

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

http://alvinalexander.com/programming/printf-format-cheat-sheet