Cldr.Number.to_string
to_string, go back to Cldr.Number module for more information.
Specs
to_string( number() | Decimal.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
numberis an integer, float or Decimal to be formattedbackendis anyCldrbackend. That is, any module that containsuse Cldroptionsis 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. SeeCldr.Number.Formatfor how format strings can be constructed. SeeCldr.Number.Format.format_styles_for/3to return available format styles for a locale. The defaultformatis:standard.If
:formatis set to:longor:shortthen the formatting depends on whether:currencyis specified. If not specified then the number is formatted as:decimal_longor:decimal_short. If:currencyis specified the number is formatted as:currency_longor:currency_shortand:fractional_digitsis set to 0 as a default.:formatmay also be a format defined by CLDR's Rules Based Number Formats (RBNF). Further information is found in the moduleCldr.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:romanor:roman_lower.currency: is the currency for which the number is formatted. For available currencies seeCldr.Currency.known_currencies/0. This option is required if:formatis set to:currency. Ifcurrencyis set and no:formatis set,:formatwill be set to:currencyas well.currency_symbol: Allows overriding a currency symbol. The alternatives are::isothe ISO currency code will be used instead of the default currency symbol.:narrowuses 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.:symboluses the standard symbol defined in CLDR. A symbol is unique for each currency and can be safely used.- "string" uses
stringas the currency symbol :standard(the default and recommended) uses the CLDR-defined symbol based upon the currency format for the locale.
:cash: a boolean which indicates whether a number being formatted as a:currencyis to be considered a cash value or not. Currencies can be rounded differently depending on whether:cashistrueorfalse. *This option is deprecated in favour ofcurrency_digits: :cash.:currency_digitsindicates which of the rounding and digits should be used. The options are:accountingwhich is the default,:cashor: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. Ifnumber_systemis anatomthennumber_systemis interpreted as a number system. SeeCldr.Number.System.number_systems_for/2. If the:number_systemisbinarythen it is interpreted as a number system name. SeeCldr.Number.System.number_system_names_for/2. The default is:default.:locale: determines the locale in which the number is formatted. SeeCldr.known_locale_names/0. The default isCldr.get_locale/0which is the locale currently in affect for thisProcessand which is set byCldr.put_locale/1.If
:fractional_digitsis 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_digitswill override that setting.If
:maximum_integer_digitsis set to a positive integer value then the numnber is left truncated before formatting. For example if the number1234is formatted with the optionmaximum_integer_digits: 2, the number is truncated to34and formatted.If
:round_nearestis 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_digitsoverrides the CLDR definition of minimum grouping digits. For example in the localeesthe number1234is formatted by default as1345because the locale defines theminimium_grouping_digitsas2. Ifminimum_grouping_digits: 1is set as an option the number is formatting as1.345. The:minimum_grouping_digitsis 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 toto_string/2.cf: defines whether to use currency or accounting format for formatting currencies. This overrides theformat: :currencyandformat: :accountingoptions.nu: defines the number system to be used if none is specified by the:number_systemoption toto_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> 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, TestBackend.Cldr, format: :roman
{:ok, "CXXIII"}
iex> Cldr.Number.to_string 123, TestBackend.Cldr, locale: "th-u-nu-thai"
{:ok, "๑๒๓"}
iex> Cldr.Number.to_string 123, TestBackend.Cldr, 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> 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
localeandnumber_system. This happens typically when the number system is:algorithmicrather 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")
{:error, {Cldr.UnknownFormatError,
"The locale \"he\" with number system :hebr does not define a format :standard"}}