Skip to content

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)

julia
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
julia
fmt_number!(
    tbl::StyledTable,
    cols;
    digits,
    trailing_zeros
) -> Any

Format values in cols to a fixed number of decimal places.

Arguments

  • tbl: the StyledTable to modify.

  • cols: column name(s) to format — a single Symbol or an AbstractVector{Symbol}.

Keywords

  • digits: number of decimal places (default 2).

  • trailing_zeros: keep trailing zeros (default true).

Returns

tbl (modified in place).

See also: fmt_percent!, fmt_integer!, fmt!.

Examples

julia
tbl = StyledTable(df)
fmt_number!(tbl, [:x]; digits = 2)
render(tbl)
source

fmt_percent!

Multiply by scale (default 100) and append suffix (default "%").

Signature: fmt_percent!(tbl, cols; digits=1, scale=100, suffix="%")

julia
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%):

julia
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
julia
fmt_percent!(
    tbl::StyledTable,
    cols;
    digits,
    scale,
    suffix
) -> Any

Format values in cols as percentage strings.

Multiplies each value by scale, formats to digits decimal places, and appends suffix.

Arguments

Keywords

  • digits: decimal places for the percentage (default 1).

  • scale: multiplier applied before formatting (default 100).

  • suffix: string appended after the number (default "%").

Returns

tbl (modified in place).

See also: fmt_number!, fmt_integer!, fmt!.

Examples

julia
tbl = StyledTable(df)
fmt_percent!(tbl, [:rate]; digits = 1)
render(tbl)
source

fmt_integer!

Round to the nearest integer and display without a decimal point.

Signature: fmt_integer!(tbl, cols)

julia
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
julia
fmt_integer!(tbl::StyledTable, cols) -> Any

Round values in cols to the nearest integer and format without a decimal point.

Arguments

Returns

tbl (modified in place).

See also: fmt_number!, fmt_percent!, fmt!.

Examples

julia
tbl = StyledTable(df)
fmt_integer!(tbl, [:count])
render(tbl)
source

fmt!

Apply a custom formatter function.

Signature: fmt!(f, tbl, cols)

julia
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:

julia
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
julia
fmt!(f::Function, tbl::StyledTable, cols) -> StyledTable

Apply 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: the StyledTable to modify.

  • cols: column name(s) to format.

Returns

tbl (modified in place).

See also: fmt_number!, fmt_percent!, fmt_integer!.

Examples

julia
tbl = StyledTable(df)
fmt!(x -> "≈$(round(Int, x))", tbl, [:x])
render(tbl)
source