Java 科学记数法中的双值格式

Java 科学记数法中的双值格式,java,formatting,scientific-notation,Java,Formatting,Scientific Notation,我有一个像223.456543434这样的双精度数字,我需要像0.223x10e+2那样显示它 如何在Java中执行此操作?来自。(复制/粘贴,因为页面似乎有问题) 您可以使用java.textpackage以科学记数法显示数字。具体来说,java.text包中的DecimalFormat类可用于此目的 以下示例显示了如何执行此操作: import java.text.*; import java.math.*; public class TestScientific { public

我有一个像
223.456543434这样的双精度数字,我需要像
0.223x10e+2那样显示它

如何在Java中执行此操作?

来自。(复制/粘贴,因为页面似乎有问题)


您可以使用
java.text
package以科学记数法显示数字。具体来说,
java.text
包中的
DecimalFormat
类可用于此目的

以下示例显示了如何执行此操作:

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
  }
}  
导致

    2.235e+02
这是我得到的最接近的


更多信息:

最后,我手工完成了:

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;
}

这个答案将为使用谷歌搜索“java科学符号”的四万多人节省时间

Y在
%X.YE
中是什么意思?

E
之间的数字是小数位(不是有效数字)。

System.out.println(String.format("%.3E",223.45654543434));
// "2.235E+02"
// rounded to 3 decimal places, 4 total significant figures
String.format
方法要求您指定要舍入的小数位数。如果需要保留原始数字的确切意义,则需要不同的解决方案

X在
%X.YE
中是什么意思?

%
之间的数字是字符串将占用的最小字符数。(此数字不是必需的,如上所示,如果省略,字符串将自动填充)

System.out.println(String.format(“%3.3E”,223.4565434));

//“2.235E+02”在正确的科学记数法中,数字是2.23e-2,而不是0.223e-3……当然我指的是2.23e+2,而不是2.23e-2。我声称这是因为操作
3
-
更容易注意到“复制/粘贴,因为页面似乎有问题”,也因为这样应该独立运行,以防外部引用移动、消失等,然而,我通常只是c/p的真正相关的部分,然后链接到原始来源更详细的信息。在本例中,原始源代码工作不正常,因此我将其全部放在这里。连续除法!?请参阅下面的答案答案根据许多因素,答案可能是“低于”、“高于”或其他任何地方,例如您如何对其进行排序(最活跃、最老、投票最多)。如果您需要参考另一个答案,请链接到该答案(单击“共享”以获得答案的永久链接)。此处给出的答案将是我的首选答案,而不是连续的除法。或
System.out.printf(“%6.3e\n”,223.23525)
请解释“%6.3e”的含义。我可以推断。3e意味着数字后面只有三位数。但是6是什么意思呢?@don喉头它意味着结果中最多有6个有意义的数字。有关更多详细信息,请查看指向格式化程序语法的链接。
System.out.println(String.format("%.3E",223.45654543434));
// "2.235E+02"
// rounded to 3 decimal places, 4 total significant figures
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