Formato de doble valor en notación científica


Tengo un número doble como 223.45654543434 y necesito mostrarlo como 0.223x10e+2.

¿Cómo puedo hacer esto en Java?

Author: HaskellElephant, 2010-05-31

4 answers

    System.out.println(String.format("%6.3e",223.45654543434));

Resultados en

    2.235e+02

Que es lo más cercano que tengo.

Más información: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

 28
Author: Peter Tillemans,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-31 16:56:26

De Muestra los números en notación científica. (Copiar / pegar porque la página parece tener problemas )


Puede mostrar números en notación científica usando el paquete java.text. Específicamente, la clase DecimalFormat en el paquete java.text se puede usar para este objetivo.

El siguiente ejemplo muestra cómo hacer esto:

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
  }
}  
 22
Author: Greg Olmstead,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-20 22:07:21

Esta respuesta ahorrará tiempo para las personas de más de 40k que están buscando en Google "notación científica java."

¿Qué significa Y en %X.YE?

El número entre . y E es el número de decimales (NO las cifras significativas).

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

El método String.format requiere que especifique el número de dígitos decimales a redondear. Si necesita preservar el significado exacto del número original, entonces necesitará un número diferente solución.

¿Qué significa X en %X.YE?

El número entre % y . es el número mínimo de caracteres que ocupará la cadena. (este número no es necesario, como se muestra arriba, la cadena se rellenará automáticamente si la omites)

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
 11
Author: Mike S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-28 22:41:48

Finalmente lo hago a mano:

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;
}
 -5
Author: Caipivara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-06 21:13:24