以科学记数法格式化双精度值

发布于 2024-09-03 19:16:11 字数 106 浏览 9 评论 0原文

我有一个双数,例如 223.45654543434,我需要将其显示为 0.223x10e+2

我怎样才能在Java中做到这一点?

I have a double number like 223.45654543434 and I need to show it like 0.223x10e+2.

How can I do this in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷路的信 2024-09-10 19:16:11
    System.out.println(String.format("%6.3e",223.45654543434));

结果

    2.235e+02

这是我得到的最接近的

。更多信息: http://java. sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

    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

愛上了 2024-09-10 19:16:11

这个答案将为 40k+ 正在谷歌搜索“java science notation”的人节省时间。

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

.E之间的数字是<的数量strong>小数位(不是有效数字)。

System.out.println(String.format("%.3E",223.45654543434));
// "2.235E+02"
// rounded to 3 decimal places, 4 total significant figures

String.format 方法要求您指定要舍入的小数位数。如果您需要保留原始数字的确切意义,那么您将需要不同的解决方案。

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

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

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

This answer will save time for the 40k+ people who are googling "java scientific notation."

What does Y mean in %X.YE?

The number between the . and E is the number of decimal places (NOT the significant figures).

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.

What does X mean in %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
写下不归期 2024-09-10 19:16:11

来自 以科学方式显示数字符号。 (复制/粘贴,因为页面似乎有问题


您可以使用 java.text 包以科学计数法显示数字。具体而言,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
  }
}  

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.

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
  }
}  
遇见了你 2024-09-10 19:16:11

最后我手工完成:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文