假设我们有以下三个值-
double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI;
现在让我们格式化这些双精度型数字。首先,我们使用Math.exp()格式化欧拉数。之后,我们还评估了日志。您可以在此处看到的%.3f是我们用来格式化数字的格式。
System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1));
以下是一个示例,其中我们还显示了其他用Java格式化double的方法。
public class Demo { public static void main(String args[]) { double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI; System.out.format("%f%n", val1); System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1)); System.out.format("%3f%n", val2); System.out.format("%+3f%n", val2); System.out.format("%f%n", val3); System.out.format("%.3f%n", val3); System.out.printf("pow(%.3f, %.3f) = %.3f%n", val1, val2, Math.pow(val1, val2)); } }
输出结果
15.546000 exp(15.546) = 5643415.357 log = 15.546 25.870000 +25.870000 3.141593 3.142 pow(15.546, 25.870) = 6716991850252629000000000000000.000