Qu

format

Builtin index · Collections & strings

result = format(x, pattern)

Input: x, pattern · Output: result

What it is

x is a number; pattern is a Str picture: "0.00", "#,##0.0", "0.0%", "0.00e0". Interpolation ({x:.2f}) reads better inline; format is for a pattern held in a variable.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = format(x, pattern)
x numberx is a number.
pattern Strpattern is a Str picture: "0.00", "#,##0.0", "0.0%", "0.00e0".
result Str
Returns a Str.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

n = parse_as("hello world 12", "hello world %d")
print(n)                          # 12

reading = scan("temp = 98.6 F", "temp = %f %s")
print(reading)                    # (98.6, "F")

print(format(1234.5, "#,##0.0"))  # "1,234.5"

print(format(0.256, "0.0%"))      # "25.6%"

output
12
(98.6, "F")
1,234.5
25.6%

from collections-strings

How it works

format(x, pattern) — a number rendered to a picture, BASIC's and Excel's. String interpolation ({x:.2f}) covers most of this and reads better inline; format is for the case interpolation cannot reach, where the pattern is in a variable. "0.00" two decimals, always "#,##0.0" thousands separators "0.0%" as a percentage "0.00e0" scientific

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.