View Source QRNBU.Types.Amount (NBU payment QR v0.3.3)

Monetary amount with validation for NBU QR codes.

Constraints

  • Minimum: 0.01 UAH
  • Maximum: 999,999,999.99 UAH
  • Precision: Exactly 2 decimal places
  • Must be positive (non-zero)

Examples

iex> QRNBU.Types.Amount.new(100.50)
{:ok, %QRNBU.Types.Amount{}}

iex> QRNBU.Types.Amount.new(Decimal.new("50.00"))
{:ok, %QRNBU.Types.Amount{}}

iex> QRNBU.Types.Amount.new(10050)  # Integer in kopiykas
{:ok, %QRNBU.Types.Amount{}}  # Converts to 100.50

iex> QRNBU.Types.Amount.new(0)
{:error, "Amount must be at least 0.01"}

iex> QRNBU.Types.Amount.new(-50)
{:error, "Amount must be positive"}

Summary

Functions

Creates a new Amount with validation.

Extracts the Decimal value from an Amount.

Converts Amount to string with exactly 2 decimal places.

Types

@opaque t()

Functions

@spec new(Decimal.t() | float() | integer() | String.t()) ::
  {:ok, t()} | {:error, String.t()}

Creates a new Amount with validation.

Accepts:

  • Decimal.t() - Direct decimal value
  • float() - Converted to Decimal
  • integer() - Treated as kopiykas (cents), divided by 100
  • String.t() - Parsed as decimal string

Returns {:ok, amount} if valid, {:error, reason} otherwise.

@spec to_decimal(t()) :: Decimal.t()

Extracts the Decimal value from an Amount.

@spec to_string(t()) :: String.t()

Converts Amount to string with exactly 2 decimal places.