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

Validates monetary amounts according to NBU QR code specifications.

Rules

  • Amount must be positive (minimum 0.01)
  • Maximum amount: 999,999,999.99 UAH
  • Exactly 2 decimal places
  • Accepts multiple input formats: Decimal, integer (kopiykas), float, string

Examples

iex> QRNBU.Validators.Amount.validate(Decimal.new("100.50"))
{:ok, #Decimal<100.50>}

iex> QRNBU.Validators.Amount.validate(10050)
{:ok, #Decimal<100.50>}

iex> QRNBU.Validators.Amount.validate("100.50")
{:ok, #Decimal<100.50>}

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

iex> QRNBU.Validators.Amount.validate("100.123")
{:error, "Amount must have at most 2 decimal places"}

Summary

Functions

Formats an amount as a string with exactly 2 decimal places.

Converts a validated amount to its kopiykas (cents) representation.

Validates and normalizes a monetary amount.

Types

@type amount_input() :: Decimal.t() | integer() | float() | String.t()

Functions

@spec format(Decimal.t()) :: String.t()

Formats an amount as a string with exactly 2 decimal places.

Examples

iex> amount = Decimal.new("100.5")
iex> QRNBU.Validators.Amount.format(amount)
"100.50"

iex> amount = Decimal.new("1000")
iex> QRNBU.Validators.Amount.format(amount)
"1000.00"
@spec to_kopiykas(Decimal.t()) :: integer()

Converts a validated amount to its kopiykas (cents) representation.

Examples

iex> amount = Decimal.new("100.50")
iex> QRNBU.Validators.Amount.to_kopiykas(amount)
10050
@spec validate(amount_input()) :: {:ok, Decimal.t()} | {:error, String.t()}

Validates and normalizes a monetary amount.

All numeric inputs are treated as hryvnias (UAH):

InputResultDescription
1010 UAHInteger as hryvnias
100.50100 UAH 50 kopFloat with kopiykas
"100,5"100 UAH 50 kopString with comma separator

Accepts multiple input types:

  • Decimal.t() - used directly
  • integer() - treated as hryvnias (e.g., 100 = 100 UAH)
  • float() - converted to Decimal (e.g., 100.50 = 100 UAH 50 kop)
  • String.t() - parsed to Decimal (supports both . and , as decimal separator)

Returns {:ok, Decimal.t()} with normalized amount or {:error, String.t()}.