使用 Java 中的新 DecimalFormat("0.#####E0") 設定 double


首先,設定雙精度值 −

double d1 = 1.39876;
double d2 = 2.39876;
double d3 = 0.66920;

現在,使用新的 DecimalFormat("0.#####E0") 格式化雙精度值 −

System.out.println(new DecimalFormat("0.#####E0").format(d1));
System.out.println(new DecimalFormat("0.#####E0").format(d2));
System.out.println(new DecimalFormat("0.#####E0").format(d3));

以下是一個示例 −

示例

 線上演示

import java.text.DecimalFormat;
public class Demo {
   public static void main(String[] argv) throws Exception {
      double d1 = 1.39876;
      double d2 = 2.39876;
      double d3 = 0.66920;
      System.out.println(new DecimalFormat("0.#####E0").format(d1));
      System.out.println(new DecimalFormat("0.#####E0").format(d2));
      System.out.println(new DecimalFormat("0.#####E0").format(d3));
   }
}

輸出

1.39876E0
2.39876E0
6.692E-1

更新時間:2019 年 7 月 30 日

146 次檢視

開啟您的 職業生涯

完成課程並獲得認證

立即開始
廣告
© . All rights reserved.