英文:
Java Print formatting decimal
问题
System.out.println("\n 两个不同的复数根存在:root1 = " +
String.format("%.4f", x1) + " + " + imaginary + "i 和 root2 = " +
String.format("%.4f", x2) + " - " + imaginary + "i");
在这里是我尝试格式化的二次公式
else if (disc < 0) {
x1 = x2 = -b / (2 * a);
imaginary = Math.sqrt(-disc) / (2 * a);
System.out.format("%.3f%n 两个不同的复数根存在:root1 = " +
String.format("%.4f", x1) + " + " + imaginary + " 和 root2 = " +
String.format("%.4f", x2) + " - " + imaginary);
}
英文:
How can I format the following to 4 decimal places. I wasn't able to use System.out.format("****")
or System.out.printf("****")
System.out.println("\n Two Distinct Complex Roots Exists: root1 = " +
x1 + " + " + imaginary + "i and root2 = " + x2 +" - " +imaginary + "i");
here is the quadratic formula that I'm trying to format
else if (disc < 0) {
x1 = x2 = -b / (2 * a);
imaginary = Math.sqrt(-disc) / (2 * a);
System.out.format("%.3f%n Two Distinct Complex Roots Exists: root1 = " +
x1 + " + " + imaginary + " and root2 = " + x2 +" - " +imaginary);
}
答案1
得分: 2
DecimalFormat df = new DecimalFormat("#.####");
System.out.println(df.format(d));
英文:
DecimalFormat df = new DecimalFormat("#.####");
System.out.println(df.format(d));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论