# `Cldr.Number`
[🔗](https://github.com/elixir-cldr/cldr_numbers/blob/v2.38.2/lib/cldr/number.ex#L1)

Formats numbers and currencies based upon CLDR's decimal formats specification.

The format specification is documentated in [Unicode TR35](http://unicode.org/reports/tr35/tr35-numbers.html#Number_Formats).
There are several classes of formatting including non-scientific, scientific,
rules based (for spelling and ordinal formats), compact formats that display `1k`
rather than `1,000` and so on.  See `Cldr.Number.to_string/2` for specific formatting
options.

### Non-Scientific Notation Formatting

The following description applies to formats that do not use scientific
notation or significant digits:

* If the number of actual integer digits exceeds the maximum integer digits,
  then only the least significant digits are shown. For example, 1997 is
  formatted as "97" if the maximum integer digits is set to 2.

* If the number of actual integer digits is less than the minimum integer
  digits, then leading zeros are added. For example, 1997 is formatted as
  "01997" if the minimum integer digits is set to 5.

* If the number of actual fraction digits exceeds the maximum fraction
  digits, then half-even rounding it performed to the maximum fraction
  digits. For example, 0.125 is formatted as "0.12" if the maximum fraction
  digits is 2. This behavior can be changed by specifying a rounding
  increment and a rounding mode.

* If the number of actual fraction digits is less than the minimum fraction
  digits, then trailing zeros are added. For example, 0.125 is formatted as
  "0.1250" if the minimum fraction digits is set to 4.

* Trailing fractional zeros are not displayed if they occur j positions after
  the decimal, where j is less than the maximum fraction digits. For example,
  0.10004 is formatted as "0.1" if the maximum fraction digits is four or
  less.

### Scientific Notation Formatting

Numbers in scientific notation are expressed as the product of a mantissa and
a power of ten, for example, 1234 can be expressed as 1.234 x 10^3. The
mantissa is typically in the half-open interval [1.0, 10.0) or sometimes
[0.0, 1.0), but it need not be. In a pattern, the exponent character
immediately followed by one or more digit characters indicates scientific
notation. Example: "0.###E0" formats the number 1234 as "1.234E3".

* The number of digit characters after the exponent character gives the
  minimum exponent digit count. There is no maximum. Negative exponents are
  formatted using the localized minus sign, not the prefix and suffix from
  the pattern. This allows patterns such as "0.###E0 m/s". To prefix positive
  exponents with a localized plus sign, specify '+' between the exponent and
  the digits: "0.###E+0" will produce formats "1E+1", "1E+0", "1E-1", and so
  on. (In localized patterns, use the localized plus sign rather than '+'.)

* The minimum number of integer digits is achieved by adjusting the exponent.
  Example: 0.00123 formatted with "00.###E0" yields "12.3E-4". This only
  happens if there is no maximum number of integer digits. If there is a
  maximum, then the minimum number of integer digits is fixed at one.

* The maximum number of integer digits, if present, specifies the exponent
  grouping. The most common use of this is to generate engineering notation,
  in which the exponent is a multiple of three, for example, "##0.###E0". The
  number 12345 is formatted using "##0.####E0" as "12.345E3".

* When using scientific notation, the formatter controls the digit counts
  using significant digits logic. The maximum number of significant digits
  limits the total number of integer and fraction digits that will be shown
  in the mantissa; it does not affect parsing. For example, 12345 formatted
  with "##0.##E0" is "12.3E3". Exponential patterns may not contain grouping
  separators.

### Significant Digits

There are two ways of controlling how many digits are shows: (a)
significant digits counts, or (b) integer and fraction digit counts. Integer
and fraction digit counts are described above. When a formatter is using
significant digits counts, it uses however many integer and fraction digits
are required to display the specified number of significant digits. It may
ignore min/max integer/fraction digits, or it may use them to the extent
possible.

# `format_type`

```elixir
@type format_type() ::
  :standard
  | :decimal_short
  | :decimal_long
  | :currency_short
  | :currency_long
  | :percent
  | :accounting
  | :scientific
  | :currency
```

# `decimal_format_metadata`

```elixir
@spec decimal_format_metadata(String.t(), Cldr.backend()) ::
  {:ok, Cldr.Number.Format.Meta.t()} | {:error, String.t()}
```

Returns the metadata representing the given
format.

# `precision`

Return the precision (number of digits) of a number.

This function delegates to `Cldr.Digits.number_of_digits/1`.

### Example

    iex> Cldr.Number.precision 1.234
    4

# `to_approx_string`

```elixir
@spec to_approx_string(number() | Decimal.t(), Cldr.backend(), Keyword.t() | map()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}
```

Formats a number and applies the `:approximately` format for
a locale and number system.

### Arguments

* `number` is an integer, float or Decimal to be formatted.

* `backend` is any `Cldr` backend. That is, any module that
  contains `use Cldr`.

* `options` is a keyword list defining how the number is to be formatted.
  See `Cldr.Number.to_string/3` for a description of the available
  options.

### Example

    iex> Cldr.Number.to_approx_string 1234, TestBackend.Cldr
    {:ok, "~1,234"}

# `to_at_least_string`

```elixir
@spec to_at_least_string(number() | Decimal.t(), Cldr.backend(), Keyword.t() | map()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}
```

Formats a number and applies the `:at_least` format for
a locale and number system.

### Arguments

* `number` is an integer, float or Decimal to be formatted

* `backend` is any `Cldr` backend. That is, any module that
  contains `use Cldr`

* `options` is a keyword list defining how the number is to be formatted.
  See `Cldr.Number.to_string/3` for a description of the available
  options.

### Example

    iex> Cldr.Number.to_at_least_string 1234, TestBackend.Cldr
    {:ok, "1,234+"}

# `to_at_most_string`

```elixir
@spec to_at_most_string(number() | Decimal.t(), Cldr.backend(), Keyword.t() | map()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}
```

Formats a number and applies the `:at_most` format for
a locale and number system.

### Arguments

* `number` is an integer, float or Decimal to be formatted.

* `backend` is any `Cldr` backend. That is, any module that
  contains `use Cldr`.

* `options` is a keyword list defining how the number is to be formatted.
  See `Cldr.Number.to_string/3` for a description of the available
  options.

### Example

    iex> Cldr.Number.to_at_most_string 1234, TestBackend.Cldr
    {:ok, "≤1,234"}

# `to_number_system`

```elixir
@spec to_number_system(number(), atom(), Cldr.backend()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}
```

Converts a number from the latin digits `0..9` into
another number system.  Returns `{:ok, string}` or
`{:error, reason}`.

### Arguments

* `number` is an integer, float.  Decimal is supported only for
  `:numeric` number systems, not `:algorithmic`.  See `Cldr.Number.System.to_system/3`
  for further information.

* `system` is any number system returned by `Cldr.known_number_systems/0`.

### Examples

    iex> Cldr.Number.to_number_system 123, :hant, TestBackend.Cldr
    {:ok, "一百二十三"}

    iex> Cldr.Number.to_number_system 123, :hebr, TestBackend.Cldr
    {:ok, "קכ״ג"}

# `to_number_system!`

```elixir
@spec to_number_system!(number(), atom(), Cldr.backend()) :: String.t() | no_return()
```

Converts a number from the latin digits `0..9` into
another number system. Returns the converted number
or raises an exception on error.

### Arguments

* `number` is an integer, float.  Decimal is supported only for
  `:numeric` number systems, not `:algorithmic`.  See `Cldr.Number.System.to_system/3`
  for further information.

* `system` is any number system returned by `Cldr.Number.System.known_number_systems/0`.

### Example

    iex> Cldr.Number.to_number_system! 123, :hant, TestBackend.Cldr
    "一百二十三"

# `to_range_string`

```elixir
@spec to_range_string(Range.t(), Cldr.backend(), Keyword.t() | map()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}
```

Formats the first and last numbers of a range and applies
the `:range` format for a locale and number system.

### Arguments

* `number` is an integer, float or Decimal to be formatted.

* `backend` is any `Cldr` backend. That is, any module that
  contains `use Cldr`.

* `options` is a keyword list defining how the number is to be formatted.
  See `Cldr.Number.to_string/3` for a description of the available
  options.

### Example

    iex> Cldr.Number.to_range_string 1234..5678, TestBackend.Cldr
    {:ok, "1,234–5,678"}

# `to_ratio_string`

```elixir
@spec to_ratio_string(number() | Decimal.t(), Cldr.backend(), Keyword.t() | map()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}
```

Formats a number as a (possibly approximate) ratio.

`Cldr.Math.float_to_ratio/2` is used to form a ratio that
represents a float number. The representation is intentionally not
precise - if a binary precise ratio is required, use `Float.ratio/1`.

The focus of this function is to produce human readable and
understandable ratios, hence the deliberately relaxed precision.

When formatting, the returned string is composed of the integer
part (if the integer is not zero) and the fractional part.

See [CLDR Rational Numbers](https://www.unicode.org/reports/tr35/dev/tr35-numbers.html#rational-numbers)
for additional information.

### Arguments

* `number` is an integer or float to be formatted. Decimal
  numbers are converted to floats before processing.

* `backend` is optional and is any `Cldr` backend. That is, any module that
  contains `use Cldr`.

* `options` is a keyword list of options.

### Options

* `:prefer` indicates a preference for formatting the ratio. It is either
  `:default`, `:super_sub` or `:precomposed`. `:precomposed` can also be
  combined with either `:default` or `:super_sub` in a list.

    * `:default` which typically uses an ascii space between
      the integer and the fraction. This is a lowest common
      denominator format for when the target rendering system
      does not format an integer and fraction between a [fractional slash](https://www.compart.com/en/unicode/U+2044)
      correctly.

    * `:super_sub` which uses [superscript and subscript](https://www.compart.com/en/unicode/block/U+2070)
      digits if the formatting number system is Latin. This format also commonly uses a
      [word joiner](https://www.compart.com/en/unicode/U+2060)
      between the integer and the fraction. This is the recommended
      option however not all rendering systems do a good job of rendering
      integers alongside fractions separated by a [fractional slash](https://www.compart.com/en/unicode/U+2044),

    * `:precomposed` will use the [Unicode Vulgar Fraction](https://www.compart.com/en/unicode/decomposition/%3Cfraction%3E)
      character if the resolved ratio is supported.

* `:max_denominator` is the largest integer permitted for
   the derived denominator. The default is 10.

* `:max_iterations` is the maximum number if iterations in the
  [continued fraction](https://en.wikipedia.org/wiki/Continued_fraction)
  calculations of the ratio. The default is 20.

* `:epsilon` is the tolerance for float comparisons when
   deriving the ration. The default is 1.0e-10.

* All other options are passed to `Cldr.Number.to_string/3` when
  formatting the integer, the numerator and the denominator.

### Returns

  * `{:ok, fraction_string}` where `fraction_string` is a string representation of an
    integer (if there is an integer part) and a ratio expressed as a fraction, or

  * `{:error, {exception, reason}}`.

### Notes

The availability of both "Integer with rational pattern" and "Integer with rational super_sub pattern"
is because ome fonts and rendering systems don’t properly handle the fraction slash, and the
user would see something like 51/2 (fifty-one halves) when 5½ is desired.

| Pattern                                   | Format         | Usage                                       | Description |
| :---------------------------------------- | :------------- | :------------------------------------------ | :---------- |
| Rational pattern                          | {0}⁄{1}        | All ratios                                  | The format for a rational fraction with arbitrary numerator and denominator; the English pattern uses the Unicode character ‘⁄’ U+2044 FRACTION SLASH which causes composition of fractions such as 22⁄7, when supported properly by rendering systems and fonts. |
| Integer with rational pattern             | {0} {1}        | prefer: :default                            | The format for combining an integer with a rational fraction that is composed using the `Rational` pattern; the English pattern uses U+202F NARROW NO-BREAK SPACE (NNBSP) to produce a _non-breaking thin space_. |
| Integer with rational "super sub" pattern | {0}⁠{1} | prefer: :super_sub and prefer: :precomposed | The format for combining an integer with a rational fraction that is composed using the rational pattern; the English pattern uses U+2060 WORD JOINER, a _zero-width no-break space_. |

### Examples

    iex> Cldr.Number.to_ratio_string(3.14159, TestBackend.Cldr)
    {:ok, "3 1⁄7"}

    iex(44)> Cldr.Number.to_ratio_string(-0.75)
    {:ok, "-3⁄4"}

    iex> Cldr.Number.to_ratio_string(3.14159, locale: :ar, number_system: :arab)
    {:ok, "٧⁄١ ٣"}

    iex> Cldr.Number.to_ratio_string(-3.14159)
    {:ok, "-3 1⁄7"}

    iex> Cldr.Number.to_ratio_string(-0.14159)
    {:ok, "-1⁄7"}

    iex> Cldr.Number.to_ratio_string(3.14159, prefer: :super_sub)
    {:ok, "3⁠¹⁄₇"}

    iex> Cldr.Number.to_ratio_string(0.5, prefer: :precomposed)
    {:ok, "½"}

    iex> Cldr.Number.to_ratio_string(1.5, prefer: :precomposed)
    {:ok, "1 ½"}

    iex> Cldr.Number.to_ratio_string(Decimal.new("1.5"), prefer: :precomposed)
    {:ok, "1 ½"}

    iex> Cldr.Number.to_ratio_string(1.5, prefer: [:super_sub, :precomposed])
    {:ok, "1⁠½"}

# `to_ratio_string!`

```elixir
@spec to_ratio_string!(number() | Decimal.t(), Cldr.backend(), Keyword.t() | map()) ::
  String.t() | no_return()
```

Formats a number as a (possibly approximate) ratio or raises
an exception.

`Cldr.Math.float_to_ratio/2` is used to form a ratio that
represents a float number. The representation is intentionally not
precise - if a binary precise ratio is required, use `Float.ratio/1`.

The focus of this function is to produce human readable and
understandable ratios, hence the deliberately relaxed precision.

When formatting, the returned string is composed of the integer
part (if the integer is not zero) and the fractional part.

### Arguments

* `number` is an integer or float to be formatted. Decimal
  numbers are converted to floats before processing.

* `backend` is optional and is any `Cldr` backend. That is, any module that
  contains `use Cldr`.

* `options` is a keyword list of options.

### Options

* `:prefer` indicates a preference for formatting the ratio. It is either
  `:default`, `:super_sub` or `:precomposed`. `:precomposed` can also be
  combined with either `:default` or `:super_sub` in a list.

    * `:default` which typically uses an ascii space between
      the integer and the fraction. This is a lowest common
      denominator format for when the target rendering system
      does not format an integer and fraction between a [fractional slash](https://www.compart.com/en/unicode/U+2044)
      correctly.

    * `:super_sub` which uses [superscript and subscript](https://www.compart.com/en/unicode/block/U+2070)
      digits if the formatting number system is Latin. This format also commonly uses a
      [word joiner](https://www.compart.com/en/unicode/U+2060)
      between the integer and the fraction. This is the recommended
      option however not all rendering systems do a good job of rendering
      integers alongside fractions separated by a [fractional slash](https://www.compart.com/en/unicode/U+2044),

    * `:precomposed` will use the [Unicode Vulgar Fraction](https://www.compart.com/en/unicode/decomposition/%3Cfraction%3E)
      character if the resolved ratio is supported.

* `:max_denominator` is the largest integer permitted for
   the derived denominator. The default is 10.

* `:max_iterations` is the maximum number if iterations in the
  [continued fraction](https://en.wikipedia.org/wiki/Continued_fraction)
  calculations of the ratio. The default is 20.

* `:epsilon` is the tolerance for float comparisons when
   deriving the ration. The default is 1.0e-10.

* All other options are passed to `Cldr.Number.to_string/3` when
  formatting the integer, the numerator and the denominator.

### Returns

    * A string representation of an integer (if there is an integer
      part) and a ratio expressed as a fraction, or

    * raises an exception.

### Notes

The availability of both "Integer with rational pattern" and "Integer with rational super_sub pattern"
is because ome fonts and rendering systems don’t properly handle the fraction slash, and the
user would see something like 51/2 (fifty-one halves) when 5½ is desired.

| Pattern                                   | Format         | Usage                                       | Description |
| :---------------------------------------- | :------------- | :------------------------------------------ | :---------- |
| Rational pattern                          | {0}⁄{1}        | All ratios                                  | The format for a rational fraction with arbitrary numerator and denominator; the English pattern uses the Unicode character ‘⁄’ U+2044 FRACTION SLASH which causes composition of fractions such as 22⁄7, when supported properly by rendering systems and fonts. |
| Integer with retional pattern             | {0} {1}        | prefer: :default                            | The format for combining an integer with a rational fraction that is composed using the `Rational` pattern; the English pattern uses U+202F NARROW NO-BREAK SPACE (NNBSP) to produce a _non-breaking thin space_. |
| Integer with rational "super sub" pattern | {0}⁠{1} | prefer: :super_sub and prefer: :precomposed | The format for combining an integer with a rational fraction that is composed using the rational pattern; the English pattern uses U+2060 WORD JOINER, a _zero-width no-break space_. |

### Examples

    iex> Cldr.Number.to_ratio_string!(3.14159, TestBackend.Cldr)
    "3 1⁄7"

    iex(44)> Cldr.Number.to_ratio_string!(-0.75)
    "-3⁄4"

    iex> Cldr.Number.to_ratio_string!(3.14159, locale: :ar, number_system: :arab)
    "٧⁄١ ٣"

    iex> Cldr.Number.to_ratio_string!(-3.14159)
    "-3 1⁄7"

    iex> Cldr.Number.to_ratio_string!(-0.14159)
    "-1⁄7"

    iex> Cldr.Number.to_ratio_string!(3.14159, prefer: :super_sub)
    "3⁠¹⁄₇"

    iex> Cldr.Number.to_ratio_string!(0.5, prefer: :precomposed)
    "½"

    iex> Cldr.Number.to_ratio_string!(1.5, prefer: :precomposed)
    "1 ½"

    iex> Cldr.Number.to_ratio_string!(Decimal.new("1.5"), prefer: :precomposed)
    "1 ½"

    iex> Cldr.Number.to_ratio_string!(1.5, prefer: [:super_sub, :precomposed])
    "1⁠½"

# `to_string`

```elixir
@spec to_string(
  number() | Decimal.t() | String.t(),
  Cldr.backend() | Keyword.t() | map(),
  Keyword.t() | map()
) :: {:ok, String.t()} | {:error, {atom(), String.t()}}
```

Returns a number formatted into a string according to a format pattern and options.

### Arguments

* `number` is an integer, float or Decimal to be formatted

* `backend` is any `Cldr` backend. That is, any module that
  contains `use Cldr`

* `options` is a keyword list defining how the number is to be formatted. The
  valid options are:

### Options

* `format`: the format style or a format string defining how the number is
  formatted. See `Cldr.Number.Format` for how format strings can be constructed.
  See `Cldr.Number.Format.format_styles_for/3` to return available format styles
  for a locale. The default `format` is `:standard`.

  * If `:format` is set to `:long` or `:short` then the formatting depends on
    whether `:currency` is specified. If not specified then the number is
    formatted as `:decimal_long` or `:decimal_short`. If `:currency` is
    specified the number is formatted as `:currency_long` or
    `:currency_short` and `:fractional_digits` is set to 0 as a default.

  * If `:format` is set to `:currency_long_with_symbol` then a format composed
    of `:currency_long` with the locale's currency format is used.

  * `:format` may also be a format defined by CLDR's Rules Based Number
    Formats (RBNF).  Further information is found in the module `Cldr.Rbnf`.
    The most commonly used formats in this category are to spell out the
    number in a locale's language.  The applicable formats are `:spellout`,
    `:spellout_year` and `:ordinal`.  A number can also be formatted as roman
    numbers by using the format `:roman` or `:roman_lower`. If the format is
    an RBNF format then the options `:gender` and`:grammatical_case` may also
    be provided. If they are provided then they will be used to try to resolve
    an available RBNF rule for the given `:locale`.

* `currency`: is the currency for which the number is formatted. If `currency`
  is set and no `:format` is set, `:format` will be set to `:currency` as well.
  Currency may be any [ISO 4217 currency code](https://en.wikipedia.org/wiki/ISO_4217)
  returned by `Cldr.Currency.known_currencies/0` or a
  [ISO 24165](https://www.iso.org/standard/80601.html) digital token
  identifier (crypto currency).

* `currency_symbol`: Allows overriding a currency symbol. The alternatives
  are:
  * `:iso` the ISO currency code will be used instead of the default
    currency symbol.
  * `:narrow` uses the narrow symbol defined for the locale. The same
    narrow symbol can be defined for more than one currency and therefore this
    should be used with care. If no narrow symbol is defined, the standard
    symbol is used.
  * `:symbol` uses the standard symbol defined in CLDR. A symbol is unique
    for each currency and can be safely used.
  * `:standard` (the default and recommended) uses the CLDR-defined symbol
    based upon the currency format for the locale.
  * `:none` means format the number without any currency symbol.
  * "string" uses `string` as the currency symbol

* `:cash`: a boolean which indicates whether a number being formatted as a
  `:currency` is to be considered a cash value or not. Currencies can be
  rounded differently depending on whether `:cash` is `true` or `false`.
  *This option is deprecated in favour of `currency_digits: :cash`. Ignored
  if the currency is a digital token.

* `:currency_digits` indicates which of the rounding and digits should be
  used. The options are `:accounting` which is the default, `:cash` or
  `:iso`. Ignored if the currency is a digital token.

* `:rounding_mode`: determines how a number is rounded to meet the precision
  of the format requested. The available rounding modes are `:down`,
  :half_up, :half_even, :ceiling, :floor, :half_down, :up. The default is
  `:half_even`.

* `:number_system`: determines which of the number systems for a locale
  should be used to define the separators and digits for the formatted
  number. If `number_system` is an `atom` then `number_system` is
  interpreted as a number system. See
  `Cldr.Number.System.number_systems_for/2`. If the `:number_system` is
  `binary` then it is interpreted as a number system name. See
  `Cldr.Number.System.number_system_names_for/2`. The default is `:default`.

* `:separators` selects which of the available symbol
  sets should be used when attempting to parse a string into a number.
  The default is `:standard`. Some limited locales have an alternative `:us`
  variant that can be used. See `Cldr.Number.Symbol.number_symbols_for/3`
  for the symbols supported for a given locale and number system.

* `:locale`: determines the locale in which the number is formatted. See
  `Cldr.known_locale_names/0`. The default is`Cldr.get_locale/0` which is the
  locale currently in affect for the current process and which is set by
  `Cldr.put_locale/1`.

* If `:fractional_digits` is set to a positive integer value then the number
  will be rounded to that number of digits and displayed accordingly - overriding
  settings that would be applied by default.  For example, currencies have
  fractional digits defined reflecting each currencies minor unit.  Setting
  `:fractional_digits` will override that setting.

* If `:maximum_integer_digits` is set to a positive integer value then the
  number is left truncated before formatting. For example if the number `1234`
  is formatted with the option `maximum_integer_digits: 2`, the number is
  truncated to `34` and formatted.

* If `:round_nearest` is set to a positive integer value then the number
  will be rounded to nearest increment of that value - overriding
  settings that would be applied by default.

* `:minimum_grouping_digits` overrides the CLDR definition of minimum grouping
  digits. For example in the locale `es` the number `1345` is formatted by default
  as `1345` because the locale defines the `minimium_grouping_digits` as `2`. If
  `minimum_grouping_digits: 1` is set as an option the number is formatted as
  `1.345`. The `:minimum_grouping_digits` is added to the grouping defined by
  the number format.  If the sum of these two digits is greater than the number
  of digits in the integer (or fractional) part of the number then no grouping
  is performed.

* `:wrapper` is a 2-arity function that will be called for each number component
  with parameters `string` and `tag` where `tag` is one of `:number`,
  `:currency_symbol`, `:currency_space`, `:literal`, `:quote`, `:percent`,
  `:permille`, `:minus` or `:plus`. The function must return either a string
  or a Phoenix safe string such as that returned by `Phoenix.HTML.Tag.content_tag/3`.
  The function can be used to wrap format elements in HTML or other tags.

### Locale extensions affecting formatting

A locale identifier can specify options that affect number formatting.
These options are:

* `nu`: defines the number system to be used if none is specified by the `:number_system`
  option to `to_string/2`

This key is part of the [u extension](https://unicode.org/reports/tr35/#u_Extension) and
that document should be consulted for details on how to construct a locale identifier with these
extensions.

### Wrapping format elements

Wrapping elements is particularly useful when formatting a number with a
currency symbol and the requirement is to have different HTML formatting
applied to the symbol than the number.  For example:

    iex> Cldr.Number.to_string(100, format: :currency, currency: :USD, wrapper: fn
    ...>   string, :currency_symbol -> "<span class=\"symbol\">" <> string <> "</span>"
    ...>   string, :number -> "<span class=\"number\">" <> string <> "</span>"
    ...>   string, :currency_space -> "<span>" <> string <> "</span>"
    ...>   string, _other -> string
    ...> end)
    {:ok, "<span class=\"symbol\">$</span><span class=\"number\">100.00</span>"}

It is also possible and recommended to use the `Phoenix.HTML.Tag.content_tag/3`
function if wrapping HTML tags since these will ensure HTML entities are
correctly encoded.  For example:

    iex> Cldr.Number.to_string(100, format: :currency, currency: :USD, wrapper: fn
    ...>   string, :currency_symbol -> Phoenix.HTML.Tag.content_tag(:span, string, class: "symbol")
    ...>   string, :number -> Phoenix.HTML.Tag.content_tag(:span, string, class: "number")
    ...>   string, :currency_space -> Phoenix.HTML.Tag.content_tag(:span, string)
    ...>   string, _other -> string
    ...> end)
    {:ok, "<span class=\"symbol\">$</span><span class=\"number\">100.00</span>"}

When formatting a number the format is parsed into format elements that might include
a currency symbol, a literal string, inserted text between a currency symbol and the
currency amount, a percent sign, the number itself and several other elements.  In
some cases it is helpful to be apply specific formatting to each element.
This can be achieved by specifying a `:wrapper` option. This option takes a 2-arity
function as an argument. For each element of the format the wrapper function is called
with two parameters:  the format element as a string and an atom representing the
element type. The wrapper function is required to return a string that is then
inserted in the final formatted number.

### RBNF with Grammatical Gender and Case

RBNF rules are used to format numbers - often to spell out a number
or format an ordinal number. In certain locales, such formats require
the specification of a grammatical gender (such as `:masculine`,
`:feminine` or `:neuter`) and/or grammatical case.

Not all locales have RBNF rules, and those that do have RBNF rules
may not have rules supporting grammatical gender or case. Therefore a
fallback mechansms is required.

* First, an attempt is made to find an RBNF rule
  that combines the required format (such as `:spellout` or `:ordinal`) and
  the specified gender and gramnmatical case.

* If not found, an attempt to find a rule that is the combnation of the
  format plus the specified gender is made.

* Finally, of not otherwise found, an attempt is made to find the format
  without the requested gender or grammatical case.

* If `;grammatical_case` is provided but not `:gender`, the `:grammatical_case`
  is ignored.

### Returns

* `{:ok, string}` or

* `{:error, {exception, message}}`

### Examples

    iex> Cldr.Number.to_string(12345, TestBackend.Cldr)
    {:ok, "12,345"}

    iex> Cldr.Number.to_string(12345, TestBackend.Cldr, locale: "fr")
    {:ok, "12 345"}

    iex> Cldr.Number.to_string(1345.32, TestBackend.Cldr, currency: :EUR, locale: "es", minimum_grouping_digits: 1)
    {:ok, "1.345,32 €"}

    iex> Cldr.Number.to_string(1345.32, TestBackend.Cldr, currency: :EUR, locale: "es")
    {:ok, "1345,32 €"}

    iex> Cldr.Number.to_string(12345, TestBackend.Cldr, locale: "fr", currency: "USD")
    {:ok, "12 345,00 $US"}

    iex> Cldr.Number.to_string(12345, TestBackend.Cldr, format: "#E0")
    {:ok, "1.2345E4"}

    iex> Cldr.Number.to_string(12345, TestBackend.Cldr, format: :accounting, currency: "THB")
    {:ok, "THB 12,345.00"}

    iex> Cldr.Number.to_string(-12345, TestBackend.Cldr, format: :accounting, currency: "THB")
    {:ok, "(THB 12,345.00)"}

    iex> Cldr.Number.to_string(12345, TestBackend.Cldr, format: :accounting, currency: "THB",
    ...> locale: "th")
    {:ok, "฿12,345.00"}

    iex> Cldr.Number.to_string(12345, TestBackend.Cldr, format: :accounting, currency: "THB",
    ...> locale: "th", number_system: :native)
    {:ok, "฿๑๒,๓๔๕.๐๐"}

    iex> Cldr.Number.to_string(1244.30, TestBackend.Cldr, format: :long)
    {:ok, "1 thousand"}

    iex> Cldr.Number.to_string(1244.30, TestBackend.Cldr, format: :long, currency: "USD")
    {:ok, "1,244 US dollars"}

    iex> Cldr.Number.to_string(1244.30, TestBackend.Cldr, format: :short)
    {:ok, "1K"}

    iex> Cldr.Number.to_string(1244.30, TestBackend.Cldr, format: :short, currency: "EUR")
    {:ok, "€1K"}

    iex> Cldr.Number.to_string(1234, TestBackend.Cldr, format: :spellout)
    {:ok, "one thousand two hundred thirty-four"}

    iex> Cldr.Number.to_string(1234, TestBackend.Cldr, format: :spellout_verbose)
    {:ok, "one thousand two hundred and thirty-four"}

    iex> Cldr.Number.to_string(1989, TestBackend.Cldr, format: :spellout_year)
    {:ok, "nineteen eighty-nine"}

    iex> Cldr.Number.to_string(123, TestBackend.Cldr, format: :ordinal)
    {:ok, "123rd"}

    iex> Cldr.Number.to_string(123, format: :ordinal, gender: :feminine, grammatical_case: :accusative, locale: :ru)
    {:ok, "123-ю"}

    iex> Cldr.Number.to_string(123, TestBackend.Cldr, format: :roman)
    {:ok, "CXXIII"}

    iex> Cldr.Number.to_string(123, TestBackend.Cldr, locale: "th-u-nu-thai")
    {:ok, "๑๒๓"}

### Errors

An error tuple `{:error, {exception, reason}}` will be returned if an error is detected.
The two most likely causes of an error return are:

  * A format cannot be compiled. In this case the error tuple will look like:

```
    iex> Cldr.Number.to_string(12345, TestBackend.Cldr, format: "0#")
    {:error, {Cldr.FormatCompileError,
      "Decimal format compiler: syntax error before: \"#\""}}
```

  * The format style requested is not defined for the `locale` and
    `number_system`. This happens typically when the number system is
    `:algorithmic` rather than the more common `:numeric`. In this case the error
    return looks like:

```
    iex> Cldr.Number.to_string(1234, TestBackend.Cldr, locale: "he", number_system: "hebr", format: :percent)
    {:error, {Cldr.UnknownFormatError,
    "The locale :he with number system :hebr does not define a format :percent"}}
```

# `to_string!`

```elixir
@spec to_string!(
  number() | Decimal.t() | String.t(),
  Cldr.backend() | Keyword.t() | map(),
  Keyword.t() | map()
) :: String.t() | no_return()
```

Same as the execution of `to_string/2` but raises an exception if an error would be
returned.

### Options

* `number` is an integer, float or Decimal to be formatted.

* `options` is a keyword list defining how the number is to be formatted. See
  `Cldr.Number.to_string/2`.

### Returns

* a formatted number as a string or

* raises an exception.

### Examples

    iex> Cldr.Number.to_string! 12345, TestBackend.Cldr
    "12,345"

    iex> Cldr.Number.to_string! 12345, TestBackend.Cldr, locale: "fr"
    "12 345"

# `validate_number_system`

```elixir
@spec validate_number_system(
  Cldr.Locale.locale_name() | Cldr.LanguageTag.t(),
  Cldr.Number.System.system_name() | Cldr.Number.System.types(),
  Cldr.backend()
) :: {:ok, Cldr.Number.System.system_name()} | {:error, {module(), String.t()}}
```

Return a valid number system from a provided locale and number
system name or type.

The number system or number system type must be valid for the
given locale.  If a number system type is provided, the
underlying number system is returned.

### Arguments

* `locale` is any valid locale name returned by `Cldr.known_locale_names/1`
  or a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/2`

* `system_name` is any number system name returned by
  `Cldr.known_number_systems/0` or a number system type
  returned by `Cldr.known_number_system_types/0`

* `backend` is any module that includes `use Cldr` and therefore
  is a `Cldr` backend module

### Examples

    iex> Cldr.Number.validate_number_system :en, :latn, TestBackend.Cldr
    {:ok, :latn}

    iex> Cldr.Number.validate_number_system :en, :default, TestBackend.Cldr
    {:ok, :latn}

    iex> Cldr.Number.validate_number_system :en, :unknown, TestBackend.Cldr
    {:error,
     {Cldr.UnknownNumberSystemError, "The number system :unknown is unknown"}}

    iex> Cldr.Number.validate_number_system "zz", :default, TestBackend.Cldr
    {:error, {Cldr.InvalidLanguageError, "The language \"zz\" is invalid"}}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
