printf輸出指定格式

2022-05-29 23:45:23 字數 647 閱讀 5315

參考部落格:

printf格式輸出:%[flags][width][.perc][f|n|h|l]type

用到了flags中的 0 (注意是零不是歐) ,其百科描述為:將輸出的前面補上0,直到佔滿指定列寬為止(不可以搭配使用-)

width 即表示需要輸出的位數。

int a = 4;

printf("%03d",a);

輸出:004

也可以用 * 代替位數,在後面的引數列表中用變數控制輸出位數;

int a

= 4;

int n

= 3;

printf("%0*d",n,a);

輸出:004

如:

int main()

輸出指定格式的日期。

程式分析 使用 datetime 模組。if name main pass code codec n input input the number n code.scoreofstudent n 輸出今日日期,格式為 dd mm yyyy。更多選項可以檢視 strftime 方法 print dat...

學習筆記 XSLT輸出指定格式

qname but not ncname version nmtoken encoding string omit xml declaration yes no standalone yes no doctype public string doctype system string cdata s...

JavaScript按指定格式輸出時間

按所給的時間格式輸出指定的時間 格式說明 對於 2014.09.05 13 14 20 yyyy 年份,2014 yy 年份,14 mm 月份,補滿兩位,09 m 月份,9 dd 日期,補滿兩位,05 d 日期,5 hh 24制小時,補滿兩位,13 h 24制小時,13 hh 12制小時,補滿兩位,...