SaltDns.Registrar.Money (Salt v1.0.0)

Money in the registrar module is always an integer in minor units (cents for EUR and USD) next to an ISO 4217 currency code. A registrar answers with decimals (11.17); they are read with Decimal and turned into minor units here, once, at the adapter's edge. No float survives past it.

The number of decimals of a currency is 2 unless listed here: 0 for currencies such as JPY and KRW, 3 for BHD, KWD, OMR and similar.

Summary

Functions

A normalised currency code ("usd" to "USD"), or nil if it is not three letters.

Decimals of a currency: 0, 2 or 3.

Builds %{minor: _, currency: _} from a registrar's price pair, a map with the keys "price" and "currency".

Minor units as a Decimal in the currency's main unit (for sending to a registrar).

Turns a registrar's number into minor units. Accepts an integer, a Decimal, a decimal string, or a float (read through its shortest text, so 11.17 is 1117 and not 1116). rounding (default :ceiling, so a cost is never understated) applies when the number has more decimals than the currency.

Types

amount()

@type amount() :: %{minor: integer(), currency: String.t()}

rounding()

@type rounding() :: :ceiling | :half_up | :floor

Functions

currency(value)

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

A normalised currency code ("usd" to "USD"), or nil if it is not three letters.

exponent(currency)

@spec exponent(String.t()) :: 0 | 2 | 3

Decimals of a currency: 0, 2 or 3.

from_price(price, rounding \\ :ceiling)

@spec from_price(term(), rounding()) :: {:ok, amount()} | :error

Builds %{minor: _, currency: _} from a registrar's price pair, a map with the keys "price" and "currency".

to_decimal(minor, currency)

@spec to_decimal(integer(), String.t()) :: Decimal.t()

Minor units as a Decimal in the currency's main unit (for sending to a registrar).

to_minor(value, currency, rounding \\ :ceiling)

@spec to_minor(term(), String.t(), rounding()) :: {:ok, integer()} | :error

Turns a registrar's number into minor units. Accepts an integer, a Decimal, a decimal string, or a float (read through its shortest text, so 11.17 is 1117 and not 1116). rounding (default :ceiling, so a cost is never understated) applies when the number has more decimals than the currency.