LineaUI.Components.NumberInput.Format (linea_ui v0.3.0)

Copy Markdown View Source

The text of a number input, worked out on the server so the first paint already shows it.

A value (an integer, a float, a numeric string or a Decimal) becomes its machine text ("1234.5": a dot, no grouping), and the machine text becomes what the person reads ("1.234,50") with the separators of a locale and a number of decimals. The browser hook does the same arithmetic in number_input_logic.js.

iex> LineaUI.Components.NumberInput.Format.machine(1234.5)
"1234.5"
iex> LineaUI.Components.NumberInput.Format.display("1234.5", %{decimal: ",", group: ".", decimals: 2, max_decimals: nil})
"1.234,50"

Summary

Functions

The number of decimals a step like "0.01" has.

What the person reads: submitted/2 with the locale's decimal separator and the whole part grouped by thousands. Text that is not a number is returned as it is; nothing is "".

The machine text of a value: digits with a dot, no grouping, or nil for nothing. A Decimal is read through its fields, so LineaUI does not depend on the library.

The decimal and thousands separators of a locale ("pt-BR", "en", "de-DE"), as {decimal, group}. Unknown locales read like English.

The machine text the form submits: rounded to decimals (always that many) or to max_decimals (at most that many, trailing zeros dropped); with neither, the number with only the decimals it has. Text that is not a number is returned as it is.

Functions

decimals_of(text)

@spec decimals_of(String.t() | nil) :: non_neg_integer()

The number of decimals a step like "0.01" has.

display(text, opts)

@spec display(String.t() | nil, map()) :: String.t()

What the person reads: submitted/2 with the locale's decimal separator and the whole part grouped by thousands. Text that is not a number is returned as it is; nothing is "".

machine(value)

@spec machine(term()) :: String.t() | nil

The machine text of a value: digits with a dot, no grouping, or nil for nothing. A Decimal is read through its fields, so LineaUI does not depend on the library.

separators(locale)

@spec separators(String.t()) :: {String.t(), String.t()}

The decimal and thousands separators of a locale ("pt-BR", "en", "de-DE"), as {decimal, group}. Unknown locales read like English.

submitted(text, opts)

@spec submitted(String.t() | nil, map()) :: String.t() | nil

The machine text the form submits: rounded to decimals (always that many) or to max_decimals (at most that many, trailing zeros dropped); with neither, the number with only the decimals it has. Text that is not a number is returned as it is.