Cldr Numbers v0.2.1 Cldr.Number.System View Source
Functions to manage number systems which describe the numbering characteristics for a locale.
A number system defines the digits (if they exist in this number system) or or rules (if the number system does not have decimal digits).
The system name is also used as a key to define the separators that are used
when formatting a number is this number_system. See
Cldr.Number.Symbol.number_symbols_for/2
.
Link to this section Summary
Functions
Return the default number system type name
Generate a transliteration map between two character classes
Returns the names of the known number systems
Returns {:ok, digits}
for a number system, or an {:error, message}
if the
number system is not know
Returns digits
for a number system, or raises an exception if the
number system is not know
Returns the actual number system from a number system type
The unique Cldr number systems names,
Returns the names of the number systems available for
a locale or an {:error, message}
tuple if the locale
is not known
Returns the names of the number systems available for
a locale or an {:error, message}
tuple if the locale
is not known
Returns a list of the known number system types
Return a map of all CLDR number systems and definitions
Returns the number systems available for a locale
or {:error, message}
if the locale is not known
Returns the number systems available for a locale or raises if the locale is not known
Returns locale and number systems that have the same digits and separators as the supplied one
Returns a number system name for a given locale and number system reference
Returns a number system name for a given locale and number system reference and raises if the number system is not available for the given locale
Number systems that have their own digit characters defined
Converts a number into the representation of a non-latin number system
Converts a number into the representation of a non-latin number system. Returns a converted string or raises on error
Returns {:ok, system}
if the number system
is known, or {:error, reason}
Link to this section Types
Link to this section Functions
Return the default number system type name.
Currently this is :default
. Note that this is
not the number system itself but the type of the
number system. It can be used to find the
default number system for a given locale with
number_systems_for(locale)[default_number_system()]
.
Example
iex> Cldr.Number.System.default_number_system_type
:default
Generate a transliteration map between two character classes
Returns the names of the known number systems.
This is the full list of number systems for which a definition is provided in CLDR.
Note that number formatting is not available for
all the number systems in this list. Number formatting
is defined for a specific list of number systems in
each locale. To determine which number systems are
supported for number formatting in a given locale see
number_system_names_for/1
.
Example
iex> Cldr.Number.System.known_number_systems
[:adlm, :ahom, :arab, :arabext, :armn, :armnlow, :bali, :beng, :bhks, :brah,
:cakm, :cham, :cyrl, :deva, :ethi, :fullwide, :geor, :grek, :greklow, :gujr,
:guru, :hanidays, :hanidec, :hans, :hansfin, :hant, :hantfin, :hebr, :hmng,
:java, :jpan, :jpanfin, :kali, :khmr, :knda, :lana, :lanatham, :laoo, :latn,
:lepc, :limb, :mathbold, :mathdbl, :mathmono, :mathsanb, :mathsans, :mlym,
:modi, :mong, :mroo, :mtei, :mymr, :mymrshan, :mymrtlng, :newa, :nkoo, :olck,
:orya, :osma, :roman, :romanlow, :saur, :shrd, :sind, :sinh, :sora, :sund,
:takr, :talu, :taml, :tamldec, :telu, :thai, :tibt, :tirh, :vaii, :wara]
Returns {:ok, digits}
for a number system, or an {:error, message}
if the
number system is not know.
Examples
iex> Cldr.Number.System.number_system_digits(:latn)
{:ok, "0123456789"}
iex> Cldr.Number.System.number_system_digits(:nope)
{:error, {Cldr.UnknownNumberSystemError, "The number system :nope is not known or does not have digits"}}
Returns digits
for a number system, or raises an exception if the
number system is not know.
Examples
iex> Cldr.Number.System.number_system_digits! :latn
"0123456789"
Cldr.Number.System.number_system_digits! :nope
** (Cldr.UnknownNumberSystemError) The number system :nope is not known or does not have digits
Returns the actual number system from a number system type.
locale
is any locale returned byCldr.Locale.new/1
system_name
is any name of name type returned byCldr.Number.System.number_system_types/0
This function will decode a number system type into the actual number system. If the number system provided can’t be decoded it is returned as is.
Examples
iex> Cldr.Number.System.number_system_for Cldr.Locale.new("th"), :default
{:ok, %{digits: "0123456789", type: :numeric}}
iex> Cldr.Number.System.number_system_for Cldr.Locale.new("th"), :native
{:ok, %{digits: "๐๑๒๓๔๕๖๗๘๙", type: :numeric}}
iex> Cldr.Number.System.number_system_for Cldr.Locale.new("th"), :latn
{:ok, %{digits: "0123456789", type: :numeric}}
iex> Cldr.Number.System.number_system_for Cldr.Locale.new("en"), :default
{:ok, %{digits: "0123456789", type: :numeric}}
iex> Cldr.Number.System.number_system_for Cldr.Locale.new("en"), :finance
{:error, {Cldr.UnknownNumberSystemError,
"The number system :finance is not known or the locale it is defined in is not known"}}
iex> Cldr.Number.System.number_system_for Cldr.Locale.new("en"), :native
{:ok, %{digits: "0123456789", type: :numeric}}
The unique Cldr number systems names,
Example
iex> Cldr.Number.System.number_system_names
[:adlm, :ahom, :arab, :arabext, :armn, :armnlow, :bali,
:beng, :bhks, :brah, :cakm, :cham, :cyrl, :deva, :ethi,
:fullwide, :geor, :grek, :greklow, :gujr, :guru, :hanidays,
:hanidec, :hans, :hansfin, :hant, :hantfin, :hebr, :hmng,
:java, :jpan, :jpanfin, :kali, :khmr, :knda, :lana, :lanatham,
:laoo, :latn, :lepc, :limb, :mathbold, :mathdbl, :mathmono,
:mathsanb, :mathsans, :mlym, :modi, :mong, :mroo, :mtei,
:mymr, :mymrshan, :mymrtlng, :newa, :nkoo, :olck, :orya,
:osma, :roman, :romanlow, :saur, :shrd, :sind, :sinh, :sora,
:sund, :takr, :talu, :taml, :tamldec, :telu, :thai, :tibt,
:tirh, :vaii, :wara]
number_system_names_for(Cldr.LanguageTag.t()) :: [String.t(), ...]
Returns the names of the number systems available for
a locale or an {:error, message}
tuple if the locale
is not known.
locale
is any locale returned byCldr.Locale.new/1
Examples
iex> Cldr.Number.System.number_system_names_for Cldr.Locale.new("en")
{:ok, [:latn]}
iex> Cldr.Number.System.number_system_names_for Cldr.Locale.new("th")
{:ok, [:latn, :thai]}
iex> Cldr.Number.System.number_system_names_for Cldr.Locale.new("he")
{:ok, [:latn, :hebr]}
iex> Cldr.Number.System.number_system_names_for Cldr.Locale.new("zz")
{:error, {Cldr.UnknownLocaleError, "The locale \"zz\" is not known."}}
Returns the names of the number systems available for
a locale or an {:error, message}
tuple if the locale
is not known.
locale
is any valid locale returned byCldr.known_locales()
Examples
iex> Cldr.Number.System.number_system_names_for! Cldr.Locale.new("en")
[:latn]
iex> Cldr.Number.System.number_system_names_for! Cldr.Locale.new("th")
[:latn, :thai]
iex> Cldr.Number.System.number_system_names_for! Cldr.Locale.new("he")
[:latn, :hebr]
Returns a list of the known number system types.
Note that not all locales support all number system types.
:default
is available for all locales, the other types
configured only in certain locales.
Example
iex> Cldr.Number.System.number_system_types
[:default, :native, :traditional, :finance]
Return a map of all CLDR number systems and definitions.
Example
iex> Cldr.Number.System.number_systems |> Enum.count
77
number_systems_for(Cldr.LanguageTag.t()) :: Map.t()
Returns the number systems available for a locale
or {:error, message}
if the locale is not known.
locale
is any locale returned byCldr.Locale.new/1
Examples
iex> Cldr.Number.System.number_systems_for Cldr.Locale.new("en")
{:ok, %{default: :latn, native: :latn}}
iex> Cldr.Number.System.number_systems_for Cldr.Locale.new("th")
{:ok, %{default: :latn, native: :thai}}
iex> Cldr.Number.System.number_systems_for Cldr.Locale.new("zz")
{:error, {Cldr.UnknownLocaleError, "The locale \"zz\" is not known."}}
number_systems_for!(Cldr.LanguageTag.t()) :: Map.t()
Returns the number systems available for a locale or raises if the locale is not known.
locale
is any locale returned byCldr.Locale.new/1
Examples
iex> Cldr.Number.System.number_systems_for! Cldr.Locale.new("en")
%{default: :latn, native: :latn}
iex> Cldr.Number.System.number_systems_for! Cldr.Locale.new("th")
%{default: :latn, native: :thai}
number_systems_like(Cldr.LanguageTag.t(), binary() | atom()) :: {:ok, List.t()} | {:error, tuple()}
Returns locale and number systems that have the same digits and separators as the supplied one.
Transliterating between locale & number systems is expensive. To avoid unncessary transliteration we look for locale and number systems that have the same digits and separators. Typically we are comparing to locale “en” and number system “latn” since this is what the number formatting routines use as placeholders.
system_name_from(binary() | atom(), Cldr.Locale.name()) :: atom()
Returns a number system name for a given locale and number system reference.
system_name
is any name of name typelocale
is any valid locale returned byCldr.known_locales()
Number systems can be references in one of two ways:
As a number system type such as :default, :native, :traditional and :finance. This allows references to a number system for a locale in a consistent fashion for a given use
WIth the number system name directly, such as :latn, :arab or any of the other 70 or so
This function dereferences the supplied system_name
and returns the
actual system name.
Examples
ex> Cldr.Number.System.system_name_from(:default,Cldr.Locale.new( "en"))
{:ok, :latn}
iex> Cldr.Number.System.system_name_from("latn", Cldr.Locale.new("en"))
{:ok, :latn}
iex> Cldr.Number.System.system_name_from(:native, Cldr.Locale.new("en"))
{:ok, :latn}
iex> Cldr.Number.System.system_name_from(:nope, Cldr.Locale.new("en"))
{:error, {Cldr.UnknownNumberSystemError,
"The number system :nope is not known or the locale it is defined in is not known"}}
Note that return value is not guaranteed to be a valid number system for the given locale as demonstrated in the third example.
Returns a number system name for a given locale and number system reference and raises if the number system is not available for the given locale.
See Cldr.Number.System.system_name_from/2
for further information.
Number systems that have their own digit characters defined.
to_system(number(), atom()) :: String.t()
Converts a number into the representation of a non-latin number system.
This function converts numbers to a known number system only, it does not provide number formatting.
There are two types of number systems in CLDR:
:numeric
in which the number system defines a direct mapping between the latin digits0..9
into a the number system equivalent. In this case,to_system/2
invokesCldr.Number.Transliterate.transliterate_digits/3
for the given number.:algorithmic
in which the number system does not have the same structure as the:latn
number system and therefore the conversion is done algorithmically. For CLDR the algorithm is implemented throughCldr.Rbnf
rulesets. These rulesets are considered by CLDR to be less rigorous than the:numeric
number systems and caution and testing for a specific use case is recommended.
Examples
iex> Cldr.Number.System.to_system 123456, :hebr
{:ok, "ק׳׳ת׳"}
iex> Cldr.Number.System.to_system 123, :hans
{:ok, "一百二十三"}
iex> Cldr.Number.System.to_system 123, :hant
{:ok, "一百二十三"}
iex> Cldr.Number.System.to_system 123, :hansfin
{:ok, "壹佰贰拾叁"}
Converts a number into the representation of a non-latin number system. Returns a converted string or raises on error.
See Cldr.Number.System.to_string/2
for further
information.
Examples
iex> Cldr.Number.System.to_system! 123, :hans
"一百二十三"
iex> Cldr.Number.System.to_system! 123, :hant
"一百二十三"
iex> Cldr.Number.System.to_system! 123, :hansfin
"壹佰贰拾叁"
Returns {:ok, system}
if the number system
is known, or {:error, reason}
Examples
iex> Cldr.Number.System.valid_number_system? :hant
{:ok, :hant}
iex> Cldr.Number.System.valid_number_system? :nope
{:error, {Cldr.UnknownNumberSystemError,
"The number system :nope is not known or the locale it is defined in is not known"}}