JAVA控制台輸出格式

來源:互聯網
上載者:User
 public class PrintFormat{ public static void main(String[] args) {  //Console print format  //System.out.printf(format, args);format為指定的輸出格式,args參數     System.out.printf("%+8.3f\n", 3.14); //"+"表示後面輸出的數字會有加號或減號,正的+,負的- ;8.3f表示輸出浮點數,寬度為8,小數點保持3位有效   System.out.printf("%+-8.3f\n", 3.14);//"-"表示靠左對齊  System.out.printf("%08.3f\n", 3.14);//"0"8位寬度中自動補0  System.out.printf("%(8.3f\n", -3.14);//"("如果是負數,自動加上( )  System.out.printf("%,f\n", 123456.78); //","金錢表示方法,每三位有一個逗號  System.out.printf("%x\n", 0x2a3b); //輸出16進位數  System.out.printf("%#x\n", 0x2a3b);//輸出帶0x標識的16進位數  System.out.printf("老闆:您名字%s,年齡:%3d歲,工資:%,-7.2f\n","ajioy",21,36000.00);  System.out.printf("老闆:您名字%1$s,年齡:%2$#x歲\n","ajioy",38); //"n{1}quot;表示用第n個參數  }} 

輸出結果如下:

 +3.140
+3.140
0003.140
   (3.140)
123,456.780000
2a3b
0x2a3b
老闆:您名字ajioy,年齡: 21歲,
老闆:您名字ajioy,年齡:0x26歲

注意:console下的輸出在實際開發中並不常用,所以理解即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.