Question

J'ai un double numéro comme 223.45654543434 et je dois le montrer comme 0.223x10e+2.

Comment puis-je faire en Java?

Était-ce utile?

La solution 4

Enfin je le fais à la main:

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

Autres conseils

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

résultats dans

    2.235e+02

qui est le plus proche que je reçois.

Plus d'info: http: / /java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

De Affichage les nombres en notation scientifique . ( Copier / coller parce que la page semble avoir des problèmes )


Vous pouvez afficher les nombres en notation scientifique à l'aide de package java.text. Plus précisément dans la classe DecimalFormat package java.text peut être utilisé pour cet objectif.

Les montre l'exemple suivant comment faire ceci:

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

Cette réponse fera gagner du temps pour les 40k + personnes qui sont googler « java notation scientifique. »

Qu'est-ce que Y signifie en %X.YE?

Le nombre entre le . et E est le nombre de décimales (pas les chiffres significatifs).

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

La méthode String.format vous oblige à indiquer le nombre de chiffres décimaux pour arrondir à. Si vous avez besoin de préserver la signification exacte du nombre initial, alors vous aurez besoin d'une autre solution.

Qu'est-ce que X en moyenne %X.YE?

Le nombre entre le % et . est le nombre minimum de caractères la chaîne prendra. (Ce nombre n'est pas nécessaire, comme indiqué ci-dessus la la chaîne remplira automatiquement si vous le laissez en )

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top