使用 java.text.DecimalFormat 格式化整數值


在此,我們使用 Java 中的 DecimalFormat 類來格式化整數,這是 java.text.DecimalFormat 包。

讓我們採用以下格式:DecimalFormat("0.######E0") 併為此目的使用 format() 方法 −

new DecimalFormat("0.#####E0").format(5089)
new DecimalFormat("0.#####E0").format(608)

由於我們在 Java 中使用了 DecimalFormat 類,因此必須匯入以下包 −

import java.text.DecimalFormat;

以下是示例 −

示例

 線上演示

import java.text.DecimalFormat;
public class Demo {
   public static void main(String[] argv) throws Exception {
      System.out.println(new DecimalFormat("0.#####E0").format(387979));
      System.out.println(new DecimalFormat("0.#####E0").format(29797));
      System.out.println(new DecimalFormat("0.#####E0").format(49788));
      System.out.println(new DecimalFormat("0.#####E0").format(5089));
      System.out.println(new DecimalFormat("0.#####E0").format(608));
      System.out.println(new DecimalFormat("0.#####E0").format(20));
   }
}

輸出

3.87979E5
2.9797E4
4.9788E4
5.089E3
6.08E2
2E1

更新於:2019 年 7 月 30 日

90 次瀏覽

啟動你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.