使用 Java System.out.format 格式化長的不同方式


Java 中使用 System.out.format 格式化輸出。在此,我們假設以下為我們的長。

long val = 787890;

為了進行格式化,我們考慮了以下內容來證明輸出。

System.out.format("%d%n", val);
System.out.format("%9d%n", val);
System.out.format("%+9d%n", val);
System.out.format("%08d%n", val);
System.out.format("%,9d%n", val);

以下是一個完整的示例,同時顯示了輸出的差異。

示例

現場演示

import java.util.Locale;
public class Demo {
   public static void main(String []args) {
      long val = 787890;
      System.out.format("%d%n", val);
      System.out.format("%9d%n", val);
      System.out.format("%+9d%n", val);
      System.out.format("%08d%n", val);
      System.out.format("%,9d%n", val);
   }
}

以下是輸出。您可以很容易地看出格式上的差異。

輸出

787890
   787890
  +787890
00787890
  787,890

更新日期: 2019-07-30

398 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.