简体   繁体   English

Java中的格式化程序

[英]Formatters in java

%b, %c, %d, %f, %s How does this work in java? %b,%c,%d,%f,%s在Java中如何工作? I have been trying to read Formatter class and formattable interface however, I am unable to understand in regards to the conversions passed as arguments. 我一直在尝试阅读Formatter类和formattable接口,但是对于作为参数传递的转换,我还是无法理解。

For example: 例如:

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

Even though formatter such as %b, %c, %d, %f, %s are limited in the ocjp6 exam, it feels like huge topic to prepare 即使ocjp6考试中的%b,%c,%d,%f,%s之类的格式化程序受到限制,这还是一个巨大的话题

I think you are having trouble understanding how System.out.printf() works. 我认为您在了解System.out.printf()的工作方式时遇到了麻烦。 It is very simple once you get the idea. 有了主意,这非常简单。

You orginal question was regarding below 您原来的问题是关于以下

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

Here System.out.printf is trying to output a String . 在这里,System.out.printf尝试输出String。 %f and %b can be understood as placeholder with special meaning. %f和%b可以被理解为具有特殊含义的占位符。

Placeholders because they will get replaced with the data that comes after comma. 占位符,因为它们将被逗号后的数据替换。 In this case %f is replaced with value of Math.PI and %b is replaced with value of Math.E 在这种情况下,%f被替换为Math.PI的值,而%b被替换为Math.E的值

Special meaning because each formatter stands for somethings for example as mention above 特殊含义,因为每个格式化程序都代表上述某些内容

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

Now to write you orginal query in a simple manner 现在以一种简单的方式来编写原始查询

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

Here %f (meaning decimal float) is replaced with float value 3.14 and %b is replaced with value "true". 此处,%f(表示十进制浮点数)被替换为浮点值3.14,%b被替换为值“ true”。

if you switch %f and %b in the above like 如果您像上面那样切换%f和%b

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

But this will work 但这会起作用

System.out.printf("%b is not equal to %f", 3.14, 3.14); // will work because 3.14 will evaluate to true The above works because of some automatic type conversion by java. //之所以起作用,是因为3.14的结果为true上面的之所以起作用,是因为java进行了一些自动类型转换。 But you can look into that later. 但是您可以稍后进行研究。

Now Regarding your last question what is happening in 现在关于您的最后一个问题

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

I am guessing you meant printf . 我猜你的意思是printf。

Now all formatters and their explanation are present in the link 现在所有格式器及其说明都出现在链接中

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

It might look intimidating . 它看起来可能令人生畏。 but it is quite easy. 但这很容易。

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

So Basically what %+04.2f means is show a positive sign for all positive numbers.The number should have 4 characters in total and two after decimal. 所以基本上%+ 04.2f的意思是对所有正数都显示一个正号。该数字总共应该有4个字符,小数点后两个。 It should be formatted as floating point number. 应将其格式化为浮点数。

More examples 更多例子

       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) 

Please go through the below links . 请通过以下链接。 It will help you understand the concept more easily 它将帮助您更轻松地理解概念

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

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

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

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

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

You can check this reference . 您可以检查此参考 Also the Oracle docs explains that in detail. Oracle文档也对此进行了详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM