У меня есть двойное число как 223.45654543434
и я должен показать его как 0.223x10e+2
.
Как я могу сделать это в Java?
Наконец, я делаю это вручную:
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;
}
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
Из Отображение чисел в научной нотации. (Копирую/вставляю, потому что со страницей, похоже, проблемы)
Вы можете отображать числа в научной нотации, используя пакет java.text
. В частности, для этого можно использовать класс DecimalFormat
в пакете java.text
.
Следующий пример показывает, как это сделать:
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
}
}