2015-04-24 58 views
0

我在這裏尋找答案,但我找不到它,如果有其他職位談論它,我很抱歉,但是,我的答案是一個容易的確定但我不能看到它,我怎麼能格式化像一個十進制:在Java中的十進制格式

7.9820892389040892803E-05 

我想格式化這個數字保持了「E」,但格式化小數有:

7.98203E-05 

如果有人會告訴我,我真的很感激它。

+0

http://obscuredclarity.blogspot.co.uk/2010/07/format-decimal-number -using-scientific.html或其他東西 – BretC

回答

3

您可以使用DecimalFormatdocs here):

Symbol Location Localized? Meaning 
0  Number  Yes   Digit 
#  Number  Yes   Digit, zero shows as absent 
.  Number  Yes   Decimal separator or monetary decimal separator 
E  Number  Yes   Separates mantissa and exponent in scientific notation. 

例如:

DecimalFormat formatter = new DecimalFormat("#.#####E00"); 
System.out.println(formatter.format(7.9820892389040892803E-05)); 
System.out.println(formatter.format(7.98E-05)); 

輸出:

7.98209E-05 
7.98E-05 

注意,當您使用#尾隨零不會被打印。如果你總是希望在小數點後五位數字,你應該使用0

DecimalFormat formatter = new DecimalFormat("0.00000E00"); 
System.out.println(formatter.format(7.98E-05)); 

輸出:

7.98000E-05 
+0

謝謝! :) – elunap

1

您可以String Formatting

你需要將%.5E(格式實現這一目標=精密5大寫科學計數法)

例如:System.out.printf("%.5E", 5/17d);打印2.94118E-01

0

使用DeciamlFormat對象...

public static void main(String[] args) { 
     double amount = 7.9820892389040892803E-05; 
     DecimalFormat df = new DecimalFormat("0.00000E0"); 
     System.out.println(df.format(amount)); 
} 

結果:

enter image description here