E-Commerce
Multi-currency catalogs, tax calculations, checkout totals
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
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 reportingSELECT region, amount, amount ->> 'USD' AS amount_usdFROM revenueORDER 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_pctFROM monthly;-- Set up exchange ratesCREATE TABLE exchange_rates ASSELECT 'EUR' as currency_from, 'USD' as currency_to, 1.085 as rate, NOW() as valid_fromUNION 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_eurFROM expenses;-- 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;-- Bad: two columns, no type safetyCREATE TABLE bad_prices ( amount DECIMAL(10,2), currency VARCHAR(3));
-- Good: single monetary columnCREATE TABLE good_prices ( amount monetary);| Use Case | Key Benefit |
|---|---|
| E-commerce | Multi-currency catalogs with safe arithmetic |
| Financial reporting | Currency conversion with auditable rates |
| Travel expenses | Automatic currency handling by country |
| Cryptocurrency | Sub-cent precision (8 decimals for BTC) |
| SaaS billing | Consistent MRR calculations |
| Double-entry ledgers | Currency validation in aggregates |