MyApp.Cldr.Number.to_string

You're seeing just the function to_string, go back to MyApp.Cldr.Number module for more information.
Link to this function

to_string(number, options \\ default_options())

View Source

Specs

to_string(number() | Decimal.t(), 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

  • options is a keyword list defining how the number is to be formatted.

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.

  • :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 the locales language. The applicable formats are :spellout, :spellout_year, :ordinal. A number can also be formatted as roman numbers by using the format :roman or :roman_lower.

  • currency: is the currency for which the number is formatted. For available currencies see Cldr.Currency.known_currencies/0. This option is required if :format is set to :currency. If currency is set and no :format is set, :format will be set to :currency as well.

  • :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.

  • :currency_digits indicates which of the rounding and digits should be used. The options are :accounting which is the default, :cash or :iso

  • :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. 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.

  • :locale: determines the locale in which the number is formatted. See Cldr.known_locale_names/0. The default isCldr.get_locale/0 which is the locale currently in affect for this 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 :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 1234 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 formatting 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.

Locale extensions affecting formatting

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

  • cu: defines what currency is implied when no curreny is specified in the call to to_string/2.

  • cf: defines whether to use currency or accounting format for formatting currencies. This overrides the format: :currency and format: :accounting options.

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

These keys are part of the u extension and that document should be consulted for details on how to construct a locale identifier with these extensions.

Returns

  • {:ok, string} or

  • {:error, {exception, message}}

Examples

iex> MyApp.Cldr.Number.to_string 12345
{:ok, "12,345"}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

iex> MyApp.Cldr.Number.to_string 123, format: :currency, locale: "en-u-cu-thb"
{:ok, "THB 123.00"}

Errors

An error tuple {:error, 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> MyApp.Cldr.Number.to_string(12345, 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> MyApp.Cldr.Number.to_string(1234, locale: "he", number_system: "hebr")
    {:error, {Cldr.UnknownFormatError,
      "The locale \"he\" with number system :hebr does not define a format :standard"}}