| vec_fmt_number_si | R Documentation |
With numeric values in a vector, we can format the values with SI (International System of Units) prefixes. These prefixes like k (kilo), M (mega), G (giga), and m (milli) scale numbers in either powers of 1000 (engineering mode) or powers of 10 and 100 (decimal mode).
We have control over the formatting with the following options:
decimals: choice of the number of decimal places, option to drop trailing zeros, and a choice of the decimal symbol
scaling: we can choose to scale targeted values by a multiplier value (useful for unit conversions)
SI prefix mode: choose between engineering prefixes (powers of 1000) or decimal prefixes (all SI prefixes)
units: an optional unit designation for the values
pattern: option to use a text pattern for decoration of the formatted values
locale-based formatting: providing a locale ID will result in formatting specific to the chosen locale
vec_fmt_number_si(
x,
unit = NULL,
decimals = 2,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
scale_by = 1,
prefix_mode = c("engineering", "decimal"),
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
incl_space = TRUE,
locale = NULL,
output = c("auto", "plain", "html", "latex", "rtf", "word")
)
x |
The input vector
This is the input vector that will undergo transformation to a character vector of the same length. Values within the vector will be formatted. |
unit |
Unit to append to formatted values
A character string specifying the unit to append after the SI prefix
(e.g., |
decimals |
Number of decimal places
The exact number of decimal places to display in the mantissa. If both
|
drop_trailing_zeros |
Drop trailing zeros
Remove trailing zeros after the decimal point (e.g., "1.50" becomes "1.5"). |
drop_trailing_dec_mark |
Drop trailing decimal mark
Remove the decimal mark if all decimal places are zero (e.g., "1." becomes "1"). |
scale_by |
Scale values by a fixed multiplier
All numeric values will be multiplied by the |
prefix_mode |
Type of SI prefixes to use
The type of SI prefixes to use. Options are |
pattern |
Decoration pattern
A formatting pattern for decorating values. Use |
sep_mark |
Thousands separator
The character to use as the thousands separator. Overridden if |
dec_mark |
Decimal mark
The character to use as the decimal point. Overridden if |
force_sign |
Force positive sign
Force the display of a plus sign for positive values. |
incl_space |
Include a space between the value and the unit symbol
An option for whether to include a space between the numerical value and
the SI prefix + unit (e.g., |
locale |
Locale identifier
An optional locale identifier that can be used for formatting values
according to the locale's rules. Examples include |
output |
Output format
The output style of the resulting character vector. This can either be
|
A character vector.
Let's create a numeric vector for the next few examples:
num_vals <- c(1.5e9, 2.7e6, 4200, 0.3, 0.00012, 2.4e-8)
Using vec_fmt_number_si() with the default options will create a
character vector with values formatted with SI prefixes. Any NA values
remain as NA values. The rendering context will be autodetected unless
specified in the output argument (here, it is of the "plain" output
type).
vec_fmt_number_si(num_vals)
#> [1] "1.50 G" "2.70 M" "4.20 k" "300.00 m" "120.00 u" "24.00 n"
We can add a unit designation to all values with the unit option:
vec_fmt_number_si(num_vals, unit = "W")
#> [1] "1.50 GW" "2.70 MW" "4.20 kW" "300.00 mW" "120.00 uW" "24.00 nW"
We can change the number of decimal places with the decimals option:
vec_fmt_number_si(num_vals, unit = "g", decimals = 0)
#> [1] "2 Gg" "3 Mg" "4 kg" "0 g" "0 g" "0 g"
If we are formatting for a different locale, we could supply the locale ID and gt will handle any locale-specific formatting options:
vec_fmt_number_si(num_vals, unit = "m", locale = "fr")
#> [1] "1,50 Gm" "2,70 Mm" "4,20 km" "300,00 mm" "120,00 um" "24,00 nm"
The scale_by option is useful for unit conversions. For instance, to
convert meters to millimeters:
vec_fmt_number_si(c(0.5, 1.2, 3.8), unit = "m", scale_by = 1000)
#> [1] "500.00 mm" "1.20 km" "3.80 km"
We can control the space between the number and unit with incl_space:
vec_fmt_number_si(num_vals, unit = "Hz", incl_space = FALSE, decimals = 1)
#> [1] "1.5GHz" "2.7MHz" "4.2kHz" "300.0mHz" "120.0uHz" "24.0nHz"
As a last example, one can wrap the values in a pattern with the pattern
argument. Note here that NA values won't have the pattern applied.
vec_fmt_number_si(num_vals, pattern = "[{x}]")
#> [1] "[1.50 G]" "[2.70 M]" "[4.20 k]" "[300.00 m]" "[120.00 u]" "[24.00 n]"
15-5
v1.2.0 (December 16, 2025)
The variant function intended for formatting gt table data:
fmt_number_si().
Other vector formatting functions:
vec_fmt_bytes(),
vec_fmt_currency(),
vec_fmt_date(),
vec_fmt_datetime(),
vec_fmt_duration(),
vec_fmt_engineering(),
vec_fmt_fraction(),
vec_fmt_index(),
vec_fmt_integer(),
vec_fmt_markdown(),
vec_fmt_number(),
vec_fmt_partsper(),
vec_fmt_percent(),
vec_fmt_roman(),
vec_fmt_scientific(),
vec_fmt_spelled_num(),
vec_fmt_time()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.