-1

%b, %c, %d, %f, %s 这在 java 中是如何工作的?我一直在尝试阅读 Formatter 类和可格式化接口,但是,我无法理解作为参数传递的转换。

例如:

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

尽管 %b, %c, %d, %f, %s 之类的格式化程序在 ocjp6 考试中受到限制,但准备起来感觉像是一个巨大的话题

4

2 回答 2

2

我认为您无法理解 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 将评估为 true 以上之所以起作用,是因为 java 进行了一些自动类型转换。但是您可以稍后再查看。

现在关于你的最后一个问题发生了什么

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

我猜你的意思是 printf 。

现在所有格式化程序及其解释都出现在链接中

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 的意思是为所有正数显示一个正号。这个数字总共应该有 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) 

请通过以下链接。它将帮助您更轻松地理解这个概念

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

于 2015-05-12T13:17:48.993 回答
0
%b is for Boolean
%f is for Decimal floating-point
%c is for Character
%d is for Decimal integer
%s is for String

你可以检查这个参考Oracle 文档也详细解释了这一点。

于 2015-05-12T09:11:21.797 回答