format-percent

Format a numeric break as a percentage.

Multiplies the value by scale (default 100) before formatting and appends suffix.

Usage

format-percent(
  scale: 100,
  suffix: "%",
  big-mark: "",
  decimal-mark: ".",
  digits: 0,
)

Parameters

Parameter Default Description
scale 100 Multiplier applied before formatting.
suffix "%" Trailing string (default "%").
big-mark "" Thousands separator.
decimal-mark "." Decimal separator.
digits 0 Decimal digits to keep.

Returns

A closure value => string.

Examples

Map proportions in [0, 1] to percent labels on the y-axis.

#plot(
  data: ((g: "a", y: 0.1), (g: "b", y: 0.45), (g: "c", y: 0.9)),
  mapping: aes(x: "g", y: "y"),
  layers: (geom-col(),),
  scales: (scale-y-continuous(labels: format-percent()),),
  width: 10cm,
  height: 6cm,
)

Bar chart of three discrete groups a, b, c with y-axis tick labels rendered as percentages from proportions via format-percent.

Bar chart of three discrete groups a, b, c with y-axis tick labels rendered as percentages from proportions via format-percent.

See also

format-number, format-currency.

Back to top