使用 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP