Localize.Number.Formatter.Ratio (Localize v0.33.0)

Copy Markdown View Source

Formats numbers as rational fractions using CLDR rational format patterns.

Rational fractions contain a numerator and denominator (e.g., ½) and may also have an integer part (e.g., 5½). The formatter supports three rendering preferences:

  • :default — fraction slash with visible space between integer and fraction (e.g., "5 1⁄2").

  • :super_sub — superscript numerator and subscript denominator with zero-width joiner (e.g., "5⁠¹⁄₂").

  • :precomposed — Unicode vulgar fraction characters when available (e.g., "5½").

Summary

Functions

Formats a number as a rational fraction string.

Functions

to_ratio_string(number, options \\ [])

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

Formats a number as a rational fraction string.

Arguments

  • number is an integer, float, or Decimal.

  • options is a keyword list of options.

Options

  • :locale is a locale identifier. The default is Localize.get_locale().

  • :prefer is a list of rendering preferences. Valid values are :default, :super_sub, and :precomposed. The default is [:default].

  • :max_denominator is the largest permitted denominator. The default is 10.

  • :max_iterations is the maximum number of continued fraction iterations. The default is 20.

  • :epsilon is the tolerance for float comparisons. The default is 1.0e-10.

Returns

  • {:ok, formatted_string} on success.

  • {:error, exception} if the number cannot be converted to a ratio or locale data is unavailable.