Skip to content

Use Cases

Real-world scenarios where monetary prevents bugs and simplifies your code.

E-Commerce

Multi-currency catalogs, tax calculations, checkout totals

Financial Reporting

Revenue consolidation, MoM comparisons, FX exposure

Travel & Expense

Multi-currency receipts, reimbursement calculations

Crypto Portfolios

BTC/ETH holdings with satoshi-level precision

DuckDB

          
DuckDB

          
CREATE TABLE revenue (
region VARCHAR,
amount monetary,
period DATE
);
INSERT INTO revenue VALUES
('North America', '1500000 USD', '2025-01-01'),
('Europe', '1200000 EUR', '2025-01-01'),
('Asia Pacific', '180000000 JPY', '2025-01-01');
-- Convert all to USD for consolidated reporting
SELECT
region,
amount,
amount ->> 'USD' AS amount_usd
FROM revenue
ORDER BY monetary_decimal(amount ->> 'USD') DESC;
WITH monthly AS (
SELECT
DATE_TRUNC('month', order_date) as month,
monetary_sum(total) as revenue
FROM orders
WHERE monetary_currency(total) = 'EUR'
GROUP BY DATE_TRUNC('month', order_date)
)
SELECT
month,
revenue,
LAG(revenue) OVER (ORDER BY month) as prev_month,
(monetary_decimal(revenue) - monetary_decimal(LAG(revenue) OVER (ORDER BY month)))
/ monetary_decimal(LAG(revenue) OVER (ORDER BY month)) * 100 as growth_pct
FROM monthly;
DuckDB

          
-- Set up exchange rates
CREATE TABLE exchange_rates AS
SELECT 'EUR' as currency_from, 'USD' as currency_to, 1.085 as rate, NOW() as valid_from
UNION ALL SELECT 'EUR', 'GBP', 0.845, NOW()
UNION ALL SELECT 'EUR', 'JPY', 162.5, NOW();
SET monetary_exchange_table = 'exchange_rates';
-- Calculate reimbursement in employee's home currency (EUR)
SELECT
description,
amount,
amount ->> 'EUR' AS reimbursement_eur
FROM expenses;
DuckDB

          
DuckDB

          

Don’t: Mix Currencies Without Converting

Section titled “Don’t: Mix Currencies Without Converting”
-- This will error (correctly!)
SELECT SUM(amount) FROM expenses;
-- Error: cannot sum EUR and USD: currency mismatch
-- Do this instead:
SELECT monetary_sum(amount ->> 'EUR') FROM expenses;

Don’t: Store Amount and Currency Separately

Section titled “Don’t: Store Amount and Currency Separately”
-- Bad: two columns, no type safety
CREATE TABLE bad_prices (
amount DECIMAL(10,2),
currency VARCHAR(3)
);
-- Good: single monetary column
CREATE TABLE good_prices (
amount monetary
);
DuckDB

          
DuckDB

          
Use CaseKey Benefit
E-commerceMulti-currency catalogs with safe arithmetic
Financial reportingCurrency conversion with auditable rates
Travel expensesAutomatic currency handling by country
CryptocurrencySub-cent precision (8 decimals for BTC)
SaaS billingConsistent MRR calculations
Double-entry ledgersCurrency validation in aggregates