私は友人から与えられた問題に取り組んでいます。x.yzw*10^p
入力番号をゼロ以外の形式で取得する必要があり、p
ゼロにx.yzw
することができます。私はプログラムを作成しましたが、問題は、 のような数値がある場合0.098
、10 進形式で作成できます9.8
が、それを取得する必要があり9.800
、常に として出力する必要があることx.yzw*10^p
です。誰かがこれがどのように可能かを教えてください。
input: output:
1234.56 1.235 x 10^3
1.2 1.200
0.098 9.800 x 10^-2
コード:
import java.util.Scanner;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class ConvertScientificNotation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.###E0");
double input = sc.nextDouble();
StringBuffer sBuffer = new StringBuffer(Double.toString(input));
sBuffer.append("00");
System.out.println(sBuffer.toString());
StringBuffer sb = new StringBuffer(df.format(Double.parseDouble(sBuffer.toString())));
if (sb.charAt(sb.length()-1) == '0') {
System.out.println(sBuffer.toString());
} else {
sb.replace(sb.indexOf("E"), sb.indexOf("E")+1, "10^");
sb.insert(sb.indexOf("10"), " x ");
System.out.println(sb.toString());
}
}
}