Formatting
Formatters convert raw cell values to display strings before styling is applied. Each formatter applies to one or more columns.
fmt_* functions leave the underlying DataFrame unchanged — they affect only the rendered output.
fmt_number!
Format to a fixed number of decimal places.
Signature: fmt_number!(tbl, cols; digits=2, trailing_zeros=true)
using StyledTables, DataFrames
df = DataFrame(x = [1.2345, 6.789], y = [100.0, 0.001])
tbl = StyledTable(df)
fmt_number!(tbl, :x; digits = 3)
fmt_number!(tbl, :y; digits = 2)
render(tbl)| x | y |
| 1.234 | 100.00 |
| 6.789 | 0.00 |
StyledTables.fmt_number! Function
fmt_number!(
tbl::StyledTable,
cols;
digits,
trailing_zeros
) -> AnyFormat values in cols to a fixed number of decimal places.
Arguments
tbl: theStyledTableto modify.cols: column name(s) to format — a singleSymbolor anAbstractVector{Symbol}.
Keywords
digits: number of decimal places (default2).trailing_zeros: keep trailing zeros (defaulttrue).
Returns
tbl (modified in place).
See also: fmt_percent!, fmt_integer!, fmt!.
Examples
tbl = StyledTable(df)
fmt_number!(tbl, [:x]; digits = 2)
render(tbl)fmt_percent!
Multiply by scale (default 100) and append suffix (default "%").
Signature: fmt_percent!(tbl, cols; digits=1, scale=100, suffix="%")
df = DataFrame(rate = [0.123, 0.456, 0.789])
tbl = StyledTable(df)
fmt_percent!(tbl, :rate; digits = 1)
render(tbl)| rate |
| 12.3% |
| 45.6% |
| 78.9% |
For already-scaled values (e.g., 12.3 stored as 12.3%):
df2 = DataFrame(rate = [12.3, 45.6, 78.9])
tbl = StyledTable(df2)
fmt_percent!(tbl, :rate; digits = 1, scale = 1)
render(tbl)| rate |
| 12.3% |
| 45.6% |
| 78.9% |
StyledTables.fmt_percent! Function
fmt_percent!(
tbl::StyledTable,
cols;
digits,
scale,
suffix
) -> AnyFormat values in cols as percentage strings.
Multiplies each value by scale, formats to digits decimal places, and appends suffix.
Arguments
tbl: theStyledTableto modify.
Keywords
digits: decimal places for the percentage (default1).scale: multiplier applied before formatting (default100).suffix: string appended after the number (default"%").
Returns
tbl (modified in place).
See also: fmt_number!, fmt_integer!, fmt!.
Examples
tbl = StyledTable(df)
fmt_percent!(tbl, [:rate]; digits = 1)
render(tbl)fmt_integer!
Round to the nearest integer and display without a decimal point.
Signature: fmt_integer!(tbl, cols)
df = DataFrame(count = [12.6, 7.2, 100.9])
tbl = StyledTable(df)
fmt_integer!(tbl, :count)
render(tbl)| count |
| 13 |
| 7 |
| 101 |
StyledTables.fmt_integer! Function
fmt_integer!(tbl::StyledTable, cols) -> AnyRound values in cols to the nearest integer and format without a decimal point.
Arguments
tbl: theStyledTableto modify.
Returns
tbl (modified in place).
See also: fmt_number!, fmt_percent!, fmt!.
Examples
tbl = StyledTable(df)
fmt_integer!(tbl, [:count])
render(tbl)fmt!
Apply a custom formatter function.
Signature: fmt!(f, tbl, cols)
df = DataFrame(p_value = [0.032, 0.001, 0.245])
tbl = StyledTable(df)
fmt!(tbl, :p_value) do pval
pval < 0.05 ? "< 0.05" : "n.s."
end
render(tbl)| p_value |
| < 0.05 |
| < 0.05 |
| n.s. |
Apply one formatter to multiple columns:
df = DataFrame(a = [1.0, 2.0], b = [3.0, 4.0])
tbl = StyledTable(df)
fmt!(tbl, [:a, :b]) do x
"$(round(Int, x)) pts"
end
render(tbl)| a | b |
| 1 pts | 3 pts |
| 2 pts | 4 pts |
StyledTables.fmt! Function
fmt!(f::Function, tbl::StyledTable, cols) -> StyledTableApply a custom formatter function to values in cols.
f receives the raw cell value and returns a display-ready value. Return x unchanged for missing to let sub_missing! handle it.
Arguments
f: formatter:f(value) -> Any.tbl: theStyledTableto modify.cols: column name(s) to format.
Returns
tbl (modified in place).
See also: fmt_number!, fmt_percent!, fmt_integer!.
Examples
tbl = StyledTable(df)
fmt!(x -> "≈$(round(Int, x))", tbl, [:x])
render(tbl)