[英]What are the semantics of java's postfix increment operator on the left side of an assignment statement and why do they differ from the right side?
[英]In the Java class DecimalFormat, what do the hashtags at the left side of the decimal do?
我對這個問題的標題不太好,但是希望我可以在本文中進一步解釋。
import java.io.*;
import java.text.*;
public class Output {
public static void main(String[] args) {
/*double number = 438.978;
/*UpperCase <- naming convention for classes.DecimalFormat x = new DecimalFormat("#.#");
System.out.println(x.format(number));*/
double number = 43.97;
DecimalFormat x = new DecimalFormat(".###");
System.out.println(x.format(number));
}
}
不要介意評論。 在Gr 11 Comp Sci課堂上,我問老師小數點左側( 第11行 )的主題標簽是否對雙精度數有任何影響,我們在課堂上進行了嘗試,發現它沒有改變System.out.println語句。
~/workspace/Java/ $ java Output
43.97
有人可以向我解釋小數點左邊的參數的用途嗎? 有人編程它做某事,所以我很好奇。
就像其他人所說的#
格式化數字,但掉零。
在您的示例中,將.###
應用於像12.34
這樣的雙12.340
格式, 它將格式化為12.340
但由於它的置零值沒有,因此不會。
當您在小數點前放置#
,例如將###.###
格式化為12.34
會顯示012.340
但由於它掉零,所以顯示為12.34
也會發生同樣的情況。
因此,小數點前的#
實際上不會執行任何操作。
的小數點前一些有用的例子,所以你可以看到格式之前小數可以工作是0
哪些格式的一個數字,但不會降為零,也可以在使用的DecimalFormat。 將000.000
類的模式應用於12.34
得到012.340
:
double d = 12.34;
DecimalFormat df = new DecimalFormat("000.000");
System.out.print(df.format(d));
在DecimalFormat中定義了0
, #
等模式。
#
格式化一個數字,但減零。 由於您的數字在小數點后有兩位數字,因此第三個格式字符將被忽略。 如果您的數字小數點后有四位數字,那么它們將被減少為三位有效數字。
根據DecimalFormat docs #
,用於有條件地顯示數字。
Symbol Location Localized? Meaning
# Number Yes Digit, zero shows as absent
請注意,已使用的#
事項的數量:
DecimalFormat x = new DecimalFormat(".###");
System.out.println(x.format(43.9711111)); // 43.971
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.