Skip to content

Latest commit

 

History

History
62 lines (48 loc) · 5.46 KB

File metadata and controls

62 lines (48 loc) · 5.46 KB

Strings

string.format(list) -> string

Format

%[.precision]conversion

Precision

Optional. In the form of a period . followed by a required positive decimal digit sequence. The default precision is 6. Not all conversions support precision.

Conversion

Character Precision Description
s N
boolThe value is formatted as true or false.
intThe value is formatted in base 10 with a preceding - if the value is negative. Insignificant 0s must be excluded.
uintThe value is formatted in base 10. Insignificant 0s must be excluded.
doubleThe value is formatted in base 10. Insignificant 0s must be excluded. If there are no significant digits after the . then it must be excluded.
bytesThe value is formatted as if string(value) was performed and any invalid UTF-8 sequences are replaced with \ufffd. Multiple adjacent invalid UTF-8 sequences must be replaced with a single \ufffd.
stringThe value is included as is.
durationThe value is formatted as decimal seconds as if the value was converted to double and then formatted as %ds.
timestampThe value is formatted according to RFC 3339 and is always in UTC.
null_typeThe value is formatted as null.
typeThe value is formatted as a string.
listThe value is formatted as if each element was formatted as "%s".format([element]), joined together with , and enclosed with [ and ].
mapThe value is formatted as if each entry was formatted as "%s: %s".format([key, value]), sorted by the formatted keys in ascending order, joined together with , , and enclosed with { and }.
d N
intThe value is formatted in base 10 with a preceding - if the value is negative. Insignificant 0s must be excluded.
uintThe value is formatted in base 10. Insignificant 0s must be excluded.
f Y int uint double: The value is converted to the style [-]dddddd.dddddd where there is at least one digit before the decimal and exactly precision digits after the decimal. If precision is 0, then the decimal is excluded. The value is rounded to the specified precision using the "round half to even" method (banker's rounding), where values exactly halfway between two numbers are rounded to the nearest even digit.
e Y int uint double: The value is converted to the style [-]d.dddddde±dd where there is one digit before the decimal and precision digits after the decimal followed by e, then the plus or minus, and then two digits.
x X N Values are formatted in base 16. For x lowercase letters are used. For X uppercase letters are used.
int uintThe value is formatted in base 16 with no insignificant digits. If the value was negative - is prepended.
stringThe value is formatted as if bytes(value) was used to convert the string to bytes and then each byte is formatted in base 16 with exactly 2 digits.
bytesThe value is formatted as if each byte is formatted in base 16 with exactly 2 digits.
o N int uint: The value is converted to base 8 with no insignificant digits. If the value was negative - is prepended.
b N int uint bool: The value is converted to base 2 with no insignificant digits. If the value was negative - is prepended.

In all cases where double is accepted: if the value is NaN the result is NaN, if the value is infinity the result is [-]Infinity.

Examples

"%s".format(["foo"])  // foo
"%s".format([b"foo"])  // foo
"%d".format([1])      // 1
"%d".format([1u])     // 1
"%d".format([3.14])   // 3.14
"%f".format([1])      // 1.000000
"%f".format([1u])     // 1.000000
"%f".format([3.14])   // 3.140000
"%.1f".format([3.14])   // 3.1
"%e".format([1])      // 1.000000e+00
"%e".format([1u])     // 1.000000e+00
"%e".format([3.14])   // 3.140000e+00
"%.1e".format([3.14])   // 3.1e+00
"%.1e".format([-3.14])   // -3.1e+00
'%.3f'.format([123.4999]) // 123.500
'%.3f'.format([123.4994]) // 123.499