gofmt
Chinese reader may go to Chinese Version.
A golang fmt.Printf
implementation in Node.js.
Please refer to http://golang.org/pkg/fmt/ for the full specification.
Quick Use
npm install gofmt --save
var sprintf =console // Hello, world!console // 72.43% %% -> %
Terms
% #-0 4 . 2 f
flags width presision verbs
flags
+ always print a sign for numeric values;
guarantee ASCII-only output for %q (%+q)
- pad with spaces on the right rather than the left (left-justify the field)
# alternate format: add leading 0 for octal (%#o), 0x for hex (%#x);
0X for hex (%#X); suppress 0x for %p (%#p);
for %q, print a raw (backquoted) string if strconv.CanBackquote
returns true;
write e.g. U+0078 'x' if the character is printable for %U (%#U).
' ' (space) leave a space for elided sign in numbers (% d);
put spaces between bytes printing strings or slices in hex (% x, % X)
0 pad with leading zeros rather than spaces;
for numbers, this moves the padding after the sign
Width and Precision
Width is specified by an optional decimal number immediately following the verb. If absent, the width is whatever is necessary to represent the value.
Precision is specified after the (optional) width by a period followed by a decimal number. If no period is present, a default precision is used (depending on different verbs). A period with no following number specifies a precision of zero. Examples:
%f: default width, default precision
%9f width 9, default precision
%.2f default width, precision 2
%9.2f width 9, precision 2
%9.f width 9, precision 0
Outputs by Verbs
%s
, %f
and %%
that presented in above example we call it verbs
.
verbs
General %v: same as %s
%T: output the type name of current operand
%%:output `%`,it does not consume operand
var sprintf =console // numberconsole // objectconsole // errorconsole // regexpconsole // arrayconsole // nullconsole // undefined
%t - Boolean
%t outputs the boolean result of the operand.
console // trueconsole // trueconsole // falseconsole // falseconsole // false
%b - Binary
For integer, output its base 2 representation. For floating-point output its decimalless scientific notation with exponent a power of two.
console // 10000000000console // 4953959590107546p-52console // -5404319552844595p-54
%c - String.fromCharCode
Output the character represented by the corresponding Unicode code point.
console // ABCDconsole // 中国
%d - Truncate a number to integer
The floating-point will be truncated but not round.
console // 1console // +1 flag '+', force to output the signconsole // ' 1' flag ' ', leave a placeholder for positive signconsole // ' 1' minimum width is 4// align to right by default// pad the remaining spaces with ' ' on the leftconsole // 0001 flag '0'// pad the remaining spaces with '0' on the leftconsole // '1 ' flag '-'// align to the left and always pad with ' '
%o - Base 8
console // 1console // 001 pad to 3 digits with '0'console // 01 flag '#'// Add a prefix '0' to the output if the first character isn't '0'.
%q
For integer, the output is a single-quoted string safely escaped with the literal expression of non-printable character.
For string, the output is a double-quoted string safely escaped with the literal expression of non-printable characters.
console // 'A'console // '\t' ASCII 7 has been converted to '\t'console // '\u038b' '\u038b' are also non-printableconsole // "\u038b\tabc"console // ' abc' No escape, but using single-quotes instead of double-quotes
%x, %X - Base 16
%x: with lower-case letters for a-f, e.g. 0x0a
.
%X: with upper-case letters for A-F, e.g. 0X0A
.
Operand is a number:
console // 10000console // 00ffconsole // 0x00ff flag '#' add '0x' as prefixconsole // -0x00ffconsole // -0X00FF
Operand is a string:
console // 616263console // 0x616263console // 61 62 63, flag ' ' separate each charactersconsole // 0x61 0x62 0x63console // 0x4e2d 0x6587 0x41 0x42 0x43
%U - Unicode format for number
console // U+0041console // U+0041 'A', flag '#'// Both the unicode format and its corresponding character will be outputtedconsole // U+0007, omit the non-printable character
%e, %E - Scientific notation:
%e: with lower-case e
, e.g. -1234.456e+78
%E: with upper-case E
, e.g. -1234.456E+78
console // 1.100000e+0console // 1.00000000000000000000e+9console // 1.100000E+0
Default precision is 6.
%f, %F - Decimal point but no exponent:
%F is synonym for %f.
console // 1.000000console // 1console // ' 1'console // ' 1.24'console // '-1.23'
Align to the left:
console // '1.00 '
Padding '0' on the left:
console // -01.235000
Round to precision:
console // 1.00console // -1.23
%g, %G - Compact output for number:
%g: whichever of %e or %f produces more compact output. %G: using 'E' in scientific notation.
console // 1console // 1.234console // 1.23, precision 被用来表示总有效位数console // 6.6666666e+6,整数部分 7 位以上转科学计数法console // 6.67e+6,precision 为科学计数法的小数部分的总有效位数console // 7e+6,precision 为 0 时
%s - String output
Operand's string output conform to the definitions of width, precision and flags.
console // any type will be convert to stringconsole // ' 12', precision in '%s' means maximum width to the operand's string representation
%v - Same as %s
%p - Does not supported
%p
is for pointer in golang, there is no counterpart in Javascript.
Bonus - Verbs not in golang
%z, %Z - Human readble filesize
%Z: Show the size by byte:
console // 1.00 kB, default is base 2,1024=1kconsole // 1.00 MBconsole // 1.0 MBconsole // 1.00 MB, flag '#' base 10,1000=1k
%z: Show the size by bits:
console // 8.00 kb, default is base 2,1024=1kconsole // 8.00 Mbconsole // 8.00 Mb, flag '#' base 10,1000=1k
Explicit Argument Indexes:
In sprintf, the default behavior is for each formatting verb to format successive arguments passed in the call. However, the notation [n] immediately before the verb indicates that the nth one-indexed argument is to be formatted instead. The same notation before a '*' for a width or precision selects the argument index holding the value. After processing a bracketed expression [n], arguments n+1, n+2, etc. will be processed unless otherwise directed.
For example,
will yield "22, 11" (index from 1), while
equivalent to
will yield " 12.00".
Because an explicit index affects subsequent verbs, this notation can be used to print the same values multiple times by resetting the index for the first argument to be repeated:
will yield "16 17 0x10 0x11".