Learn R Programming

mapgl (version 0.3)

number_format: Create a number formatting expression

Description

This function creates a number formatting expression that formats numeric values according to locale-specific conventions. It can be used in tooltips, popups, and text fields for symbol layers.

Usage

number_format(
  column,
  locale = "en-US",
  style = "decimal",
  currency = NULL,
  unit = NULL,
  minimum_fraction_digits = NULL,
  maximum_fraction_digits = NULL,
  minimum_integer_digits = NULL,
  use_grouping = NULL,
  notation = NULL,
  compact_display = NULL
)

Value

A list representing the number-format expression.

Arguments

column

The name of the column containing the numeric value to format. Can also be an expression that evaluates to a number.

locale

A string specifying the locale to use for formatting (e.g., "en-US", "de-DE", "fr-FR"). Defaults to "en-US".

style

The formatting style to use. Options include:

  • "decimal" (default): Plain number formatting

  • "currency": Currency formatting (requires currency parameter)

  • "percent": Percentage formatting (multiplies by 100 and adds %)

  • "unit": Unit formatting (requires unit parameter)

currency

For style = "currency", the ISO 4217 currency code (e.g., "USD", "EUR", "GBP").

unit

For style = "unit", the unit to use (e.g., "kilometer", "mile", "liter").

minimum_fraction_digits

The minimum number of fraction digits to display.

maximum_fraction_digits

The maximum number of fraction digits to display.

minimum_integer_digits

The minimum number of integer digits to display.

use_grouping

Whether to use grouping separators (e.g., thousands separators). Defaults to TRUE.

notation

The formatting notation. Options include:

  • "standard" (default): Regular notation

  • "scientific": Scientific notation

  • "engineering": Engineering notation

  • "compact": Compact notation (e.g., "1.2K", "3.4M")

compact_display

For notation = "compact", whether to use "short" (default) or "long" form.

Examples

Run this code
# Basic number formatting with thousands separators
number_format("population")

# Currency formatting
number_format("income", style = "currency", currency = "USD")

# Percentage with 1 decimal place
number_format("rate", style = "percent", maximum_fraction_digits = 1)

# Compact notation for large numbers
number_format("population", notation = "compact")

# Using within a tooltip
concat("Population: ", number_format("population", notation = "compact"))

# Using with get_column()
number_format(get_column("value"), style = "currency", currency = "EUR")

Run the code above in your browser using DataLab