Java 以科学记数法格式化双精度值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2944822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format double value in scientific notation
提问by Daniel Gomez Rico
I have a double number like 223.45654543434
and I need to show it like 0.223x10e+2
.
我有一个双数223.45654543434
,我需要像0.223x10e+2
.
How can I do this in Java?
我怎样才能在 Java 中做到这一点?
采纳答案by Daniel Gomez Rico
Finally I do it by hand:
最后我手工完成:
public static String parseToCientificNotation(double value) {
int cont = 0;
java.text.DecimalFormat DECIMAL_FORMATER = new java.text.DecimalFormat("0.##");
while (((int) value) != 0) {
value /= 10;
cont++;
}
return DECIMAL_FORMATER.format(value).replace(",", ".") + " x10^ -" + cont;
}
回答by Greg Olmstead
From Display numbers in scientific notation. (Copy/pasting because the page seems to be having issues)
从以科学记数法显示数字。(复制/粘贴,因为页面似乎有问题)
You can display numbers in scientific notation using java.text
package. Specifically DecimalFormat
class in java.text
package can be used for this aim.
您可以使用java.text
包以科学记数法显示数字。包中的特定DecimalFormat
类java.text
可用于此目的。
The following example shows how to do this:
以下示例显示了如何执行此操作:
import java.text.*;
import java.math.*;
public class TestScientific {
public static void main(String args[]) {
new TestScientific().doit();
}
public void doit() {
NumberFormat formatter = new DecimalFormat();
int maxinteger = Integer.MAX_VALUE;
System.out.println(maxinteger); // 2147483647
formatter = new DecimalFormat("0.######E0");
System.out.println(formatter.format(maxinteger)); // 2,147484E9
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(maxinteger)); // 2.14748E9
int mininteger = Integer.MIN_VALUE;
System.out.println(mininteger); // -2147483648
formatter = new DecimalFormat("0.######E0");
System.out.println(formatter.format(mininteger)); // -2.147484E9
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(mininteger)); // -2.14748E9
double d = 0.12345;
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(d)); // 1.2345E-1
formatter = new DecimalFormat("000000E0");
System.out.println(formatter.format(d)); // 12345E-6
}
}
回答by Peter Tillemans
System.out.println(String.format("%6.3e",223.45654543434));
results in
结果是
2.235e+02
which is the closest I get.
这是我得到的最接近的。
more info : http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
更多信息:http: //java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
回答by Mike S
This answer will save time for the 40k+ people who are googling "java scientific notation."
这个答案将为 40k+ 正在谷歌搜索“java 科学记数法”的人节省时间。
What does Y mean in %X.YE
?
Y 是什么意思%X.YE
?
The number between the .
and E
is the number of decimal places (NOT the significant figures).
.
和之间的E
数字是小数位数(不是有效数字)。
System.out.println(String.format("%.3E",223.45654543434));
// "2.235E+02"
// rounded to 3 decimal places, 4 total significant figures
The String.format
method requires you to specify the number of decimal digits to round to. If you need to preserve the exact significance of the original number then you will need a different solution.
该String.format
方法要求您指定要舍入到的小数位数。如果您需要保留原始数字的确切意义,那么您将需要不同的解决方案。
What does X mean in %X.YE
?
X 在 中是什么意思%X.YE
?
The number between the %
and .
is the minimum number of characters the string will take up.(this number is not necessary, as shown above the the string will automatically fill if you leave it out)
%
和之间的数字是字符串将占用.
的最少字符数。(这个数字不是必须的,如上图所示,如果你省略它,字符串会自动填充)
System.out.println(String.format("%3.3E",223.45654543434));
// "2.235E+02" <---- 9 total characters
System.out.println(String.format("%9.3E",223.45654543434));
// "2.235E+02" <---- 9 total characters
System.out.println(String.format("%12.3E",223.45654543434));
// " 2.235E+02" <---- 12 total characters, 3 spaces
System.out.println(String.format("%12.8E",223.45654543434));
// "2.23456545E+02" <---- 14 total characters
System.out.println(String.format("%16.8E",223.45654543434));
// " 2.23456545E+02" <---- 16 total characters, 2 spaces