Format Specifications

Format Specifications

QTAwkfollows the Draft ANSI C language standard for the format string in theprintfandfprintffunctions except for thePandntypes. SinceQTAwkdoes not support pointers, the use of these types will give unpredictable results.

A format specification has the form:

%[flags][width][.precision][h | l | L]type

which is matched by the following regular expression:

/%{flags}?{width}?{precision}?[hlL]?{type}/

with:

flags = /[-+\s#0]/;

width = /([0-9]+|\*)/;

precision = /(\.([0-9]+|\*))/;

type = /[diouxXfeEgGcs]/;

Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, '[hlL]', determines whether the associated argument is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). The optional fields control other aspects of the formatting, as follows:

flagsControl justification of output and printing of signs, blanks, decimal points, octal and hexadecimal prefixes.
widthControl minimum number of characters output.
precisionControls maximum number of characters printed for all or part of the output field, or minimum number of digits printed for integer values.
h, l, LPrefixes that determine size of argument expected (this field is retained only for compatibility to C format strings).
hUsed as a prefix with the integer types d, i, o, x, and X to specify that the argument is short int, or with u to specify a short unsigned int
lUsed as a prefix with d, i, o, x, and X types to specify that the argument is long int, or with u to specify a long unsigned int; also used as a prefix with e, E, f, g, and G types to specify a double, rather than a float
LUsed as a prefix with e, E, f, g, and G types to specify a long double

If a percent sign, '%', is followed by a character that has no meaning as a format field, the character is simply copied to the output. For example, to print a percent-sign character, use "%%".

Output Types

Type
Character
Output Action
dinteger, Signed decimal integer
iinteger, Signed decimal integer
uinteger, Unsigned decimal integer
ointeger, Unsigned octal integer
xinteger, Unsigned hexadecimal integer, using "abcdef"
Xinteger, Unsigned hexadecimal integer, using "ABCDEF"
ffloat, Signed value having the form:

[-]dddd.dddd

where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.

efloat, Signed value having the form:

[-]d.dddde(+|-)ddd,

where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits.

Efloat, Signed value having the form:

[-]d.ddddE(+|-)ddd,

gfloat, Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than -4 or greater than the precision argument. Trailing zeros are truncated and the decimal point appears only if one or more digits follow it.
Gfloat, Identical to the g format, except that 'E' introduces the exponent (where appropriate) instead of 'e'.
ccharacter, Single character
sstring, Characters printed up to the first null character ('\0') or until the precision value is reached.

Output Flags

Flag
Character
Output Action
'-'Left justify the result within the given field width. Default: Right justify.
'+'Prefix the output value with a sign (+ or -) if the output value is of a signed type. Default: Sign appears only for negative signed values (-).
blank (' ')Prefix the output value with a blank if the output value is signed and positive. The blank is ignored if both the blank and + flags appear. Default: No blank.
'#'When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively. Default: No prefix.
'#'When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases. Default: Decimal point appears only if digits follow it.
'#'When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros. Default: Decimal point appears only if digits follow it. Trailing zeros are truncated.
'#'Ignored when used with c, d, i, u or s
'0' (zero)For d, i, o, u, x, X, e, E, f, g, and G conversions, leading zeros (following any indication of sign or base) are used to pad to the field width; no space padding is performed. If the 0 and - flags both appear, the 0 flag will be ignored. For d, i, o, u, x, and X conversions, if a precision is specified, the 0 flag will be ignored. For other conversions the behavior is undefined. Default: Use blank padding

If the argument corresponding to a floating-point specifier is infinite or indefinite, the following output is produced:

+ infinity ==> 1.#INFrandom-digits

- infinity ==> -1.#INFrandom-digits

Indefinite ==> digit.#INDrandom-digits

Output Width

The width argument is a non-negative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values (depending on whether the - flag is specified) until the minimum width is reached. If the width is prefixed with a 0 flag, zeros are added until the minimum width is reached (not useful for left-justified numbers).

The width specification never causes a value to be truncated; if the number of characters in the output value is greater than the specified width, or the width is not given, all characters of the value are printed (subject to the precision specification).

The width specification may be an asterisk (*), in which case an integer argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause a truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.

Output Precision

The precision specification is a non-negative decimal integer preceded by a period, '.', which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits. Unlike the width specification, the precision can cause truncation of the output value, or rounding in the case of a floating-point value.

The precision specification may be an asterisk, '*', in which case an integer argument from the argument list supplies the value. The precision argument must precede the value being formatted in the argument list.

The interpretation of the precision value, and the default when precision is omitted, depend on the type, as described below:

d
i
u
o
x
X
The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. Default: appears without a number following it, the precision is set to 1.
e
E
The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. Default: appears without a number following it, no decimal point is printed.
fThe precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. Default: appears without a number following it, no decimal point appears.
g
G
The precision specifies the maximum number of significant digits printed. Default: Six significant digits are printed, without any trailing zeros that are truncated.
cNo effect. Default: Character printed
sThe precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. Default: All characters of the string are printed.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值