更新時間:2022-09-30 09:49:01 來源:動力節(jié)點 瀏覽1215次
假設(shè)我們有以下兩個值。
double val1 = 20.932;
double val2 = 11.67;
現(xiàn)在讓我們格式化這些浮點數(shù)。首先,我們用Math.exp() 格式化歐拉數(shù)。之后,我們還評估了 log。您可以在此處看到的 %.3f 是我們用于格式化數(shù)字的。
System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1));
System.out.printf("log = %.3f%n", val1, Math.log(val1));
以下是一個示例,我們還展示了在 Java 中格式化 float 的其他方法。
例子
public class Demo {
public static void main(String args[]) {
double val1 = 20.932;
double val2 = 11.67;
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.printf("pow(%.3f, %.3f) = %.3f%n", val1, val2, Math.pow(val1, val2));
}
}
輸出
20.932000
exp(20.932) = 1232117412.659
log = 20.932
11.670000
+11.670000
pow(20.932, 11.670) = 2593350237975675.500
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743