科学表記法の2倍の値をフォーマットします
-
05-10-2019 - |
質問
私は次のような2倍の数字を持っています 223.45654543434
そして、私はそれを見せる必要があります 0.223x10e+2
.
Javaでこれを行うにはどうすればよいですか?
解決 4
最後に私は手作業でそれをします:
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
}
}
この答えは、「Java Scientific Notation」をグーグルで調べている40k以上の人々の時間を節約します。
yとはどういう意味ですか %X.YE
?
間の数 .
と E
の数です 小数の場所(重要な数字ではありません)。
System.out.println(String.format("%.3E",223.45654543434));
// "2.235E+02"
// rounded to 3 decimal places, 4 total significant figures
String.format
メソッドでは、丸い10進数の数を指定する必要があります。元の数字の正確な重要性を維持する必要がある場合は、別のソリューションが必要になります。
xとはどういう意味ですか %X.YE
?
間の数 %
と .
それは 文字列が取り上げる最小文字の数。 (この番号は必要ありません。上に示すように、文字列はそれを除外すると自動的に埋められます)
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
所属していません StackOverflow