Localize.Number.Format.Options (Localize v0.50.0)

Copy Markdown View Source

Validation and resolution of number formatting options.

validate_options/2 resolves a keyword list of the options accepted by Localize.Number.to_string/2 into a Localize.Number.Format.Options.t/0 struct — locale validation, number system resolution, format pattern lookup, currency data loading and symbol resolution all happen once. Passing the resulting struct to Localize.Number.to_string/2 bypasses that resolution on every call, which is significantly faster when formatting many numbers with the same locale and format. See the Performance and optimization section of the Number Formatting guide for benchmarks.

Summary

Functions

Validates and resolves number formatting options into an Options struct.

Types

t()

@type t() :: %Localize.Number.Format.Options{
  currency: term(),
  currency_digits: term(),
  currency_format: term(),
  currency_spacing: term(),
  currency_symbol: term(),
  exponent_style: term(),
  format: term(),
  fractional_digits: term(),
  grammatical_case: term(),
  grammatical_gender: term(),
  locale: term(),
  max_fractional_digits: term(),
  maximum_integer_digits: term(),
  maximum_significant_digits: term(),
  min_fractional_digits: term(),
  minimum_grouping_digits: term(),
  minimum_significant_digits: term(),
  number_system: term(),
  pattern: term(),
  round_nearest: term(),
  rounding_mode: term(),
  separators: term(),
  symbols: term(),
  wrapper: term()
}

Functions

validate_options(number, options)

@spec validate_options(number() | Decimal.t(), Keyword.t()) ::
  {:ok, t()} | {:error, Exception.t()}

Validates and resolves number formatting options into an Options struct.

This function performs locale validation, number system resolution, format pattern lookup, currency data loading, and symbol resolution. The resulting struct can be passed directly to Localize.Number.to_string/2 to bypass options resolution on each call.

This is useful when formatting many numbers with the same locale and format. See the Performance and optimization section of the Number Formatting guide for benchmarks and usage guidance.

Arguments

  • number is a representative number used to determine the sign pattern. Use 0 for a positive-number format or -1 for a negative-number format.

  • options is a keyword list of the same options accepted by Localize.Number.to_string/2.

Returns

Examples

iex> {:ok, options} = Localize.Number.Format.Options.validate_options(0, locale: :en)
iex> {:ok, _} = Localize.Number.to_string(1234.56, options)