Format double value in scientific notation
Asked Answered
D

4

32

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?

Dedifferentiation answered 31/5, 2010 at 16:37 Comment(2)
In proper scientific notation the number will be 2.23e-2, not 0.223e-3.Kneepad
… and of course I mean 2.23e+2, not 2.23e-2. I claim that's because OP the 3 is easier to notice than the -.Kneepad
D
-10

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;
}
Dedifferentiation answered 2/6, 2010 at 13:24 Comment(3)
successive division!? see answer belowApothegm
Answers may be "below", "above", or just anywhere else depending on many factors, for example how you sort them (most active, oldest, most voted). If you need to refer to another answer, link to it (click 'share' to get a permalink to the answer)Quadrat
Answer given here https://mcmap.net/q/408214/-format-double-value-in-scientific-notation would be my preference rather than successive division.Florescence
B
39
    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

Batholomew answered 31/5, 2010 at 16:56 Comment(3)
Or System.out.printf("%6.3e\n", 223.23525)Rodneyrodolfo
Please explain what the "%6.3e" means. I can deduce that .3e means that there's only three digits after the . but what does the 6 mean?Whidah
@DonLarynx it means there are maximum 6 meaningful digits in the result. For more details check the link to the syntax for the formatter.Batholomew
V
28

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
Vituline answered 22/9, 2016 at 22:34 Comment(0)
S
23

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
  }
}  
Saccharine answered 31/5, 2010 at 16:41 Comment(2)
"Copy/pasting because the page seems to be having issues" Also because SO should stand on its own, in case external references move, go away, etc.Danutadanya
true enough, however i usually just c/p the really pertinent parts and then link to the original source for more detailed info. In this case, the original source is not working well, so I put it all in here.Saccharine
D
-10

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;
}
Dedifferentiation answered 2/6, 2010 at 13:24 Comment(3)
successive division!? see answer belowApothegm
Answers may be "below", "above", or just anywhere else depending on many factors, for example how you sort them (most active, oldest, most voted). If you need to refer to another answer, link to it (click 'share' to get a permalink to the answer)Quadrat
Answer given here https://mcmap.net/q/408214/-format-double-value-in-scientific-notation would be my preference rather than successive division.Florescence

© 2022 - 2024 — McMap. All rights reserved.