Skip to main content

Financial Functions

Money amount and currency functions for financial applications.

Quick Reference

FunctionDescriptionSQL Signature
anofox_money_is_positiveCheck positive(amount) -> BOOLEAN
anofox_money_is_valid_amountCheck range(amount, min, max) -> BOOLEAN
anofox_money_formatFormat currency(amount, currency) -> VARCHAR
anofox_currency_is_validValidate ISO code(code) -> BOOLEAN
anofox_currency_convertConvert currency(amount, from, to) -> DECIMAL
anofox_money_percentageCalculate percentage(amount, percent) -> DECIMAL

Amount Functions (6)

anofox_money_is_positive

Check if amount is positive (> 0).

Parameters

ParameterTypeRequiredDefaultDescription
amountDECIMALYes-Amount to check

Example

SELECT
amount,
anofox_money_is_positive(amount) as is_positive
FROM transactions;

-- 100.50 | true
-- 0.00 | false
-- -50.00 | false

anofox_money_is_valid_amount

Check if amount is within valid range.

Parameters

ParameterTypeRequiredDefaultDescription
amountDECIMALYes-Amount to validate
minDECIMALYes-Minimum valid
maxDECIMALYes-Maximum valid

Example

SELECT
anofox_money_is_valid_amount(100.50, 0.01, 999999.99), -- true
anofox_money_is_valid_amount(0.00, 0.01, 999999.99); -- false

anofox_money_normalize

Normalize amount to specific decimal places.

SELECT
anofox_money_normalize(100.556, 2), -- 100.56
anofox_money_normalize(100.1, 2); -- 100.10

anofox_money_format

Format amount with currency symbol.

Parameters

ParameterTypeRequiredDefaultDescription
amountDECIMALYes-Amount to format
currencyVARCHARYes-ISO 4217 code

Example

SELECT
anofox_money_format(100.50, 'USD'), -- '$100.50'
anofox_money_format(100.50, 'EUR'), -- '€100,50'
anofox_money_format(100.50, 'GBP'); -- '£100.50'

anofox_money_abs

Get absolute value.

SELECT anofox_money_abs(-100.50);  -- 100.50

anofox_money_round

Round to nearest cent.

SELECT anofox_money_round(100.556, 2);  -- 100.56

Currency Functions (5)

anofox_currency_is_valid

Check if currency code is valid ISO 4217.

SELECT
anofox_currency_is_valid('USD'), -- true
anofox_currency_is_valid('XXX'); -- false

anofox_currency_get_name

Get currency name.

SELECT anofox_currency_get_name('USD');  -- 'US Dollar'

anofox_currency_get_symbol

Get currency symbol.

SELECT anofox_currency_get_symbol('EUR');  -- '€'

anofox_currency_get_decimals

Get standard decimal places.

SELECT
anofox_currency_get_decimals('USD'), -- 2
anofox_currency_get_decimals('JPY'); -- 0

anofox_currency_is_crypto

Check if cryptocurrency.

SELECT
anofox_currency_is_crypto('BTC'), -- true
anofox_currency_is_crypto('USD'); -- false

Conversion & Arithmetic (6)

anofox_currency_convert

Convert amount from one currency to another.

Parameters

ParameterTypeRequiredDefaultDescription
amountDECIMALYes-Amount to convert
from_codeVARCHARYes-Source currency
to_codeVARCHARYes-Target currency

Speed: ~50ms (calls exchange rate API)

Example

SELECT
anofox_currency_convert(100, 'USD', 'EUR'), -- ~92
anofox_currency_convert(100, 'USD', 'USD'); -- 100

Arithmetic Functions

-- Add amounts
SELECT anofox_money_add(100.50, 50.25); -- 150.75

-- Subtract
SELECT anofox_money_subtract(100.50, 50.25); -- 50.25

-- Multiply by factor
SELECT anofox_money_multiply(100.00, 1.1); -- 110.00

-- Divide
SELECT anofox_money_divide(100.00, 2); -- 50.00

-- Percentage
SELECT anofox_money_percentage(100.00, 10); -- 10.00

Practical Patterns

Multi-Currency Consolidation

CREATE TABLE transactions_eur AS
SELECT
transaction_id,
amount,
currency,
anofox_currency_convert(amount, currency, 'EUR') as amount_eur
FROM transactions
WHERE anofox_currency_is_valid(currency);

Amount Validation Pipeline

SELECT
invoice_id,
amount,
currency,
CASE
WHEN NOT anofox_currency_is_valid(currency) THEN 'INVALID_CURRENCY'
WHEN NOT anofox_money_is_positive(amount) THEN 'NEGATIVE_AMOUNT'
WHEN NOT anofox_money_is_valid_amount(amount, 0.01, 999999.99) THEN 'OUT_OF_RANGE'
ELSE 'VALID'
END as validation_status,
anofox_money_format(amount, currency) as formatted_amount
FROM invoices;

Tax Calculation

SELECT
order_id,
subtotal,
tax_rate,
anofox_money_percentage(subtotal, tax_rate) as tax_amount,
anofox_money_add(subtotal, anofox_money_percentage(subtotal, tax_rate)) as total
FROM orders;

🍪 Cookie Settings