results in "inf"
or "-inf" with %f (INF or -INF with %F).
"0.0 / 0.0" results in "nan" with %f (NAN with %F).
Example: >vim
echo printf("%.2f", 12.115)
< 12.12
Note that roundoff depends on the system libraries.
Use |round()| when in doubt.
*printf-e* *printf-E*
e E The Float argument is converted into a string of the
form 1.234e+03 or 1.234E+03 when using 'E'. The
precision specifies the number of digits after the
decimal point, like with 'f'.
*printf-g* *printf-G*
g G The Float argument is converted like with 'f' if the
value is between 0.001 (inclusive) and 10000000.0
(exclusive). Otherwise 'e' is used for 'g' and 'E'
for 'G'. When no precision is specified superfluous
zeroes and '+' signs are removed, except for the zero
immediately after the decimal point. Thus 10000000.0
results in 1.0e7.
*printf-%*
% A '%' is written. No argument is converted. The
complete conversion specification is "%%".
When a Number argument is expected a String argument is also
accepted and automatically converted.
When a Float or String argument is expected a Number argument
is also accepted and automatically converted.
Any other argument type results in an error message.
*E766* *E767*
The number of {exprN} arguments must exactly match the number
of "%" items. If there are not sufficient or too many
arguments an error is given. Up to 18 arguments can be used.
*printf-$*
In certain languages, error and informative messages are
more readable when the order of words is different from the
corresponding message in English. To accommodate translations
having a different word order, positional arguments may be
used to indicate this. For instance: >vim
#, c-format
msgid "%s returning %s"
msgstr "waarde %2$s komt terug van %1$s"
<
In this example, the sentence has its 2 string arguments
reversed in the output. >vim
echo printf(
"In The Netherlands, vim's creator's name is: %1$s %2$s",
"Bram", "Moolenaar")
< In The Netherlands, vim's creator's name is: Bram Moolenaar >vim
echo printf(
"In Belgium, vim's creator's name is: %2$s %1$s",
"Bram", "Moolenaar")
< In Belgium, vim's creator's name is: Moolenaar Bram
Width (and precision) can be specified using the '*' specifier.
In this case, you must specify the field width position in the
argument list. >vim
echo printf("%1$*2$.*3$d", 1, 2, 3)
< 001 >vim
echo printf("%2$*3$.*1$d", 1, 2, 3)
< 2 >vim
echo printf("%3$*1$.*2$d", 1, 2, 3)
< 03 >vim
echo printf("%1$*2$.*3$g", 1.4142, 2, 3)
< 1.414
You can mix specifying the width and/or precision directly
and via positional arguments: >vim
echo printf("%1$4.*2$f", 1.4142135, 6)
< 1.414214 >vim
echo printf("%1$*2$.4f", 1.4142135, 6)
< 1.4142 >vim
echo printf("%1$*2$.*3$f", 1.4142135, 6, 2)
< 1.41
You will get an overflow error |E1510|, when the field-width
or precision will result in a string longer than 1 MB
(1024*1024 = 1048576) chars.
*E1500*
You cannot mix positional and non-positional arguments: >vim
echo printf("%s%1$s", "One", "Two")
< E1500: Cannot mix positional and non-positional arguments:
%s%1$s
*E1501*
You cannot skip a positional argument in a format string: >vim
echo printf("%3$s%1$s", "One", "Two", "Three")
< E1501: format argument 2 unused in $-style format:
%3$s%1$s
*E1502*
You can re-use a [field-width] (or [precision]) argument: >vim
echo printf("%1$d at width %2$d is: %01$*2$d", 1, 2)
< 1 at width 2 is: 01
However, you can't use it as a different type: >vim
echo printf("%1$d at width %2$ld is: %01$*2$d", 1, 2)
< E1502: Positional argument 2 used as field width reused as
different type: long int/int
*E1503*
When a positional argument is used, but not the correct