stringFormat
Syntax
stringFormat(format, [args...])
Arguments
format is a string containing zero or more placeholders.
args... (optional) is one or more values to fill in the placeholder(s) in format. If specified, the number and data type of args must be consistent with the number and data types of the placeholders in format; if not specified, the function outputs format directly.
Details
stringFormat
formats strings by replacing placeholders with values
passed by the user. Formatting options (e.g. field width, precision, alignment) can
be specified for more precise control over how the values are formatted in the
output strings.
Table 1. Supported data types
Type | Placeholder (%-formatting) | Examples of args |
---|---|---|
BOOL | %b | 1b, 0b, true, false |
CHAR | %c | 'a', 97c |
SHORT | %h | 122h |
integer (INT) | %i | 21 |
octal | %o | 31 |
hexadecimal (lowercase) | %x | 2f |
hexadecimal (uppercase) | %X | 2F |
LONG | %l | 25l |
DATE | %d | 2022.01.01 |
MONTH | %M | 2022.05M |
TIME | %t | 13:00:10.706 |
MINUTE | %m | 13:30m |
SECOND | %s | 13:30:10 |
DATETIME | %D | 2012.06.13 13:30:10, 2012.06.13T13:30:10 |
TIMESTAMP | %T | 2012.06.13 13:30:10.008, 2012.06.13T13:30:10.008 |
NANOTIME | %n | 13:30:10.008007006 |
NANOTIMESTAMP | %N | 2012.06.13 13:30:10.008007006, 2012.06.13T13:30:10.008007006 |
FLOAT | %f | 2.1f |
DOUBLE | %F | 2.1 |
SYMBOL | %S | symbol(["aaa", "bbb"]) |
STRING | %W | "Hello" |
ANY (tuple) | %A | (1, 45, 'sah') |
You can specify formatting options inside the placeholders like
%[(var)][#][±][0][m/*][.][n/*]type
.
Table 2. The following table lists the options which can be inserted before the
decimal point .
in placeholders:
Specifier | Meaning | Examples |
---|---|---|
m (a positive integer) | [Used with %f, %F or %W] For FLOAT (f) and DOUBLE (F) data types, m indicates the minimum total width of the output string.
|
stringFormat("%10f", pi) output:
stringFormat("%2f", 12345.0) output:
stringFormat("%10W",
"6chars") output:
|
* | Like m, * indicates the minimum total width
of the output string. However, * allows passing the width as an
argument (args). Specify the width in the corresponding
argument (args) in tuple format:
(width,value) . |
stringFormat("%*f",
(10,pi)) output:
|
0 | 0 pads numeric values with zeros. For left-aligned fields, the zeros are padded on the right side. If 0 is not specified, the output string is padded with spaces. | stringFormat("%010f", pi) output:
|
- | - left-aligns the output string within the specified field width. | stringFormat("%-10.3f",
pi) output: |
+ | + adds a plus sign "+ "
before positive values. |
stringFormat('%+f', pi) output:
|
(var) | [Cannot be used with other specifiers] (var)
allows you to format a string using a dictionary, where the
dictionary keys act as variables in the string. To specify a
key, put it in parentheses after the % symbol. The values
in the dictionary are substituted into the string where the
|
employee = {"name":"Lisa Mill", "year":2010}
stringFormat("%(name)W joined the company in %(year)i",
employee) output: |
# | [Used with %o, %x or %X] # adds "0o" before octal values; adds "0x" (lower case) or "0X" (upper case) before hexadecimal values. |
stringFormat("%#o", 33) output:
stringFormat("%#X",
33) output: |
Table 3. The following table lists the options which can be inserted after the
decimal point .
in placeholders (these options can only be used
with %f, %F or %W):
Specifier | Meaning | Examples |
---|---|---|
n (a positive integer) | For FLOAT (f) and DOUBLE (F) data types, n
specifies the number of digits after the decimal point.
|
stringFormat("%10.5f",
pi) output: stringFormat('%10.3f' , 3.1) output:
stringFormat("%2.10W",
"6chars") output: |
* | Like n, * specifies the number of digits
after the decimal point. However, * allows specifying the number of
decimals (precision) by passing it as an argument
(args).Specify the digits in the corresponding argument in
tuple format: ([width],[precision],value) . |
Specify precision: stringFormat("%.*f",
(5,pi)) output: stringFormat("%0*.*f", (10,5,pi)) output:
|
Examples
stringFormat("date: %d, time: %t", 2022.12.01, 10:12:45.065)
// output
date: 2022.12.01, time: 10:12:45.065
stringFormat("Students account for %i%% of our customers.", 50)
// output
Students account for 50% of our customers.
t = datetime(now())
stringFormat("The current time is %D.", t)
// output
The current time is 2023.01.02T20:36:03.
a = 7.596
stringFormat("%-+10.5f", a)
// output
+7.59600
stringFormat("%010.3f", a)
// output
000007.596
product = {"item":"Eggs", "price_per_unit":2}
stringFormat("%(item)W: $ %(price_per_unit)i", product)
// output
Eggs: $ 2