2014-09-04 23 views
-2

當我點擊計算,以顯示它顯示了以下的答案的計算:的Java:只顯示兩個十進制數

淨工資31030.38076923077

我怎樣才能使它只能顯示小數點後兩位十進制數。例如$ 31030.38

+0

顯示的代碼。 – Amadan 2014-09-04 06:31:34

+1

向我們顯示您的代碼 - 至少有兩種或三種替代方法,但最容易使用的方法取決於您已有的內容... – thkala 2014-09-04 06:31:35

+0

使用'DecimalFormat'類或使用'%.2f'轉換'printf ' – jackarms 2014-09-04 06:32:29

回答

0

使用DecimalFormat

double f = 31030.38076923077; 

DecimalFormat df = new DecimalFormat("#.00"); 
System.out.println(f+" "+df.format(f)); 
+0

因爲你有美好的一天,cheersssss很好用 – CaseyT 2014-09-04 06:48:33

0

使用的BigDecimal像當前顯示此

double d = 31030.38474923077; 
BigDecimal dec = new BigDecimal(d).setScale(2, RoundingMode.HALF_UP); 
System.out.println(dec); 
+0

你可能想寫一些關於'BigDecimal'的好處和缺點的文章。 *有*需要'BigDecimal'的地方,但是隻要*這樣的編碼和性能就可以保證安全'*通常*不合理...... – thkala 2014-09-04 06:41:58

相關問題