Skip to contents

Generates a function that formats numeric values as percentage strings with optional digits, spacing, and multiplication by 100.

Usage

fmt_percent(digits = 0, space = TRUE, multiply_by_100 = TRUE)

Arguments

digits

Integer indicating the number of decimal places. Defaults to 0.

space

Logical indicating whether to include a space before the percent sign. Defaults to TRUE.

multiply_by_100

Logical indicating whether to multiply the input value by 100. Defaults to TRUE.

Value

A function that takes a numeric value and returns a formatted percentage string.

Examples

# Create a formatter with default settings
formatter <- fmt_percent()
formatter(0.75)  # "75 %"
#> [1] "75 %"

# Create a formatter with 2 decimal places
formatter2 <- fmt_percent(digits = 2)
formatter2(0.12345)  # "12.35 %"
#> [1] "12.35 %"

# Create a formatter without space before the percent sign
formatter3 <- fmt_percent(space = FALSE)
formatter3(0.5)  # "50%"
#> [1] "50%"