2015-04-24 57 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