IEEE printf specification

來源:互聯網
上載者:User

敗家蘋果參考直接引用http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html 文檔來說明格式化的使用,可該文檔沒有微軟的MSDN說的清楚明確,看看下面MSDN給出的格式說明可以說明一切。

%[flags][width][.perc] [F|N|h|l]type

1.type 含義如下:

  d 有符號10進位整數

  i 有符號10進位整數

  o 有符號8進位整數

  u 無符號10進位整數

  x 無符號的16進位數字,並以小寫abcdef表示

  X 無符號的16進位數字,並以大寫ABCDEF表示

  F/f 浮點數

  E/e 用科學表示格式的浮點數

  g 使用%f和%e表示中的總的位元表示最短的來表示浮點數 G 同g格式,但表示為指數

  c 單個字元

  s 字串

  % 顯示百分比符號本身

  p 顯示一個指標,near指標表示為:XXXX

  far 指標表示為:XXXX:YYYY

  n 相連參量應是一個指標,其中存放已寫字元的個數

2.flags 規定輸出格式,取值和含義如下:

  無 靠右對齊,左邊填充0和空格

  - 靠左對齊,右邊填充空格

  + 在數字前增加符號 + 或 -

  一個空格 只對負數顯示符號

  # 當type=c,s,d,i,u時沒有影響

  type=o,x,X時,分別在數值前增加'0',"0x","0X"

  type=e,E,f時,總是使用小數點

  type=g,G時,除了數值為0外總是顯示小數點

3.width 用於控制顯示數值的寬度,取值和含義如下n(n=1,2,3...) 寬度至少為n位,不夠以空格填充0n(n=1,2,3...) 寬度至少為n位,不夠左邊以0填充 * 格式列表中

 

4.prec 用於控制小數點後面的位元,取值和含義如下:

  無 按預設精度顯示

  0 當type=d,i,o,u,x時,沒有影響

  type=e,E,f時,不顯示小數點

  n(n=1,2,3...) 當type=e,E,f時表示的最大小數位元

  type=其他,表示顯示的最大寬度 .*格式列表中

5.F|N|h|l 表示指標是否是遠指標或整數是否是長整數

  F 遠指標

  n 近指標

  h短整數或單精確度浮點數

  l 長整數或雙精確度浮點數

再來看看官方網站如何說的,明顯沒有上面的直觀。

The format can contain either numbered argument conversion specifications (that is, "%n$" and "*m$"), or unnumbered argument conversion specifications (that is, % and * ), but not both. The only exception to this is that %% can be mixed with the "%n$" form. The results of mixing numbered and unnumbered argument specifications in a format string are undefined. When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string.

In format strings containing the "%n$" form of conversion specification, numbered arguments in the argument list can be referenced from the format string as many times as required. 

In format strings containing the % form of conversion specification, each conversion specification uses the first unused argument in the argument list.

[CX]  All forms of the fprintf() functions allow for the insertion of a language-dependent radix character in the output string. The radix character is defined in the program's locale (category LC_NUMERIC ). In the POSIX locale, or in a locale where the radix character is not defined, the radix character shall default to a period ( '.' ). 

 

Each conversion specification is introduced by the '%' character [XSI]   or by the character sequence "%n$",  after which the following appear in sequence:

  • Zero or more flags (in any order), which modify the meaning of the conversion specification.

  • An optional minimum field width. If the converted value has fewer bytes than the field width, it shall be padded with spaces by default on the left; it shall be padded on the right if the left-adjustment flag ( '-' ), described below, is given to the field width. The field width takes the form of an asterisk ( '*' ), described below, or a decimal integer.

  • An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers; the number of digits to appear after the radix character for the a, A, e, E, f, and F conversion specifiers; the maximum number of significant digits for the g and G conversion specifiers; or the maximum number of bytes to be printed from a string in the s [XSI]   and S   conversion specifiers. The precision takes the form of a period ( '.' ) followed either by an asterisk ( '*' ), described below, or an optional decimal digit string, where a null digit string is treated as zero. If a precision appears with any other conversion specifier, the behavior is undefined.

  • An optional length modifier that specifies the size of the argument.

  • A conversion specifier character that indicates the type of conversion to be applied.

A field width, or precision, or both, may be indicated by an asterisk ( '*' ). In this case an argument of type int supplies the field width or precision. Applications shall ensure that arguments specifying field width, or precision, or both appear in that order before the argument, if any, to be converted. A negative field width is taken as a '-' flag followed by a positive field width. A negative precision is taken as if the precision were omitted. [XSI]   In format strings containing the "%n$" form of a conversion specification, a field width or precision may be indicated by the sequence "*m$", where m is a decimal integer in the range [1,{NL_ARGMAX}] giving the position in the argument list (after the format argument) of an integer argument containing the field width or precision, for example: 

NSLog(@"%1$d05d:%2$.*3$f:%4$.*1$d", 2, 0.5555756, 6, 3); // 00002:0.555576:03

 

(@"s%02d", 3) // 03 

(@"s%2d", 3) // _3

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.