format
format, function
def const pure format(d: date, pattern: text): text
def const pure format(m: month, pattern: text): text
def const pure format(w: week, pattern: text): text
def const pure format(n: number, pattern: text): text
def const pure format(n: i64, pattern: text): text
def const pure format(n: f64, pattern: text): text
Formats the calendar value d, m, or w, or the number n, as text according to pattern.
d: date to format.m: month to format.w: week to format.n: number to format.pattern: pattern controlling the rendered digits and separators. Calendar patterns are listed in datetimeformat.
Examples
d = date(2022, 9, 26)
table T = with
[| as Pattern |]
[| "yyyy-MM-dd" |]
[| "M/dd/yy" |]
[| "MMM d, yyyy" |]
T.Text = format(d, T.Pattern)
show table "Formats" with
T.Pattern
T.Text
This produces the following table:
| Pattern | Text |
|---|---|
| yyyy-MM-dd | 2022-09-26 |
| M/dd/yy | 9/26/22 |
| MMM d, yyyy | Sep 26, 2022 |
m = month(2024, 2)
w = week(2024, 3)
show summary "Formats (month/week)" with
format(m, "yyyy-MM")
format(w, "yyyy-MM-dd")
For numbers, pattern controls zero-padding, decimal places, digit grouping,
and percentage formatting.
table Formats = with
[| as Value, as Pattern |]
[| 12.3, "000.00" |]
[| 1234.5, "#,##0.00" |]
[| 0.256, "0.0%" |]
Formats.Text = format(Formats.Value, Formats.Pattern)
show table "Number formats" with
Formats.Value
Formats.Pattern
Formats.Text
This produces the following table:
| Value | Pattern | Text |
|---|---|---|
| 12.3 | 000.00 | 012.30 |
| 1234.5 | #,##0.00 | 1,234.50 |
| 0.256 | 0.0% | 25.6% |
Remarks
The list of supported calendar specifiers is described in datetimeformat. The month and week overloads format the start of the period.
In an Envision numeric pattern, 0 emits a digit or pads with zero, while # emits a
digit only when needed. A period marks the decimal separator, a comma between
digit placeholders groups thousands, and % multiplies the value by 100 and
appends a percent sign.
Text interpolation accepts the same calendar and numeric patterns.