簡體   English   中英

Java中的格式化程序

[英]Formatters in java

%b,%c,%d,%f,%s在Java中如何工作? 我一直在嘗試閱讀Formatter類和formattable接口,但是對於作為參數傳遞的轉換,我還是無法理解。

例如:

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

即使ocjp6考試中的%b,%c,%d,%f,%s之類的格式化程序受到限制,這還是一個巨大的話題

我認為您在了解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

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

您可以檢查此參考 Oracle文檔也對此進行了詳細說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM