Skip to content

CkMoney

Job: read, convert, format, round and split amounts of money the way payment processors handle them — safely, and without ever losing a cent.

Money data arrives in messy shapes: '$1,234.56', 'USD 10.99', the accounting negative '(123.45)', a plain number from a database. CkMoney reads them all exactly, digit by digit, and hands each answer back in the shape that keeps it exact. PHP has no decimal type, so the shape depends on where the value goes next: Amount answers a float, the nearest one to the amount read, for a value to show or compare; Format and every calculation — Multiply, Divide, Subtract, Convert, Total, Round, FromMinorUnits — answer numeric text with the currency's decimals, '21.64', exact and never through a float, ready for a DECIMAL column and read back by every method here; MinorUnits answers the int a processor charges in; and ToMoney answers the Money object itself, the amount and its currency as one value, for a long chain of arithmetic. When a value cannot be read, the number methods answer your fallback (0 unless you say otherwise), the string methods answer '' or the text '0', and Split answers an empty array — nothing throws unless you ask.

Currency codes follow ISO 4217 — the international standard of three-letter codes like 'USD', 'EUR', 'JPY' that payment systems key on.

Quick look

use SafeCoder\CommonKit\CkMoney;

CkMoney::Amount('$1,234.56');     // -> 1234.56
CkMoney::MinorUnits('$10.99');    // -> 1099  (the cents a processor charges)
CkMoney::Format(10);              // -> '10.00'  (the shape a payment API expects)
CkMoney::Currency('usd');         // -> 'USD'
CkMoney::Amount('banana');        // -> 0.0  (unreadable — the default fallback)
CkMoney::IsRounded('10.999');     // -> false  (more decimals than US dollars have)
CkMoney::IsAtLeast(
    input: '$50.00',
    min: '$10.00'
);                                // -> true
CkMoney::SameAmount(
    a: '$1,234.56',
    b: 1234.56
);                                // -> true
CkMoney::Multiply(
    input: '$19.99',
    factor: 1.0825
);                                // -> '21.64'  (with 8.25 percent tax)
CkMoney::Total(['$1,234.56', '(12.00)']);   // -> '1222.56'
CkMoney::Allocate(
    input: 100.01,
    ratios: [70, 30]
);                                // -> ['70.01', '30.00']  (never loses a cent)
CkMoney::Divide(
    input: '$100',
    divisor: 3
);                                // -> '33.33'
CkMoney::Subtract(
    input: '$100.00',
    amount: '$25.50'
);                                // -> '74.50'
CkMoney::Convert(
    input: '$100',
    rate: 0.92,
    from: 'USD',
    to: 'EUR'
);                                // -> '92.00'  (at your rate)
CkMoney::Display(
    input: 1234.5,
    currency: 'EUR'
);                                // -> '1,234.50 EUR'  (for people, not machines)
CkMoney::ToMoney('$19.99');       // -> Money USD 19.99  (for a chain of calculations)
CkMoney::CurrencyOf('Romania');   // -> 'RON'
CkMoney::Decimals('JPY');         // -> 0
CkMoney::NumericCode('EUR');      // -> '978'  (what a card network carries)
CkMoney::Currency('978');         // -> 'EUR'
CkMoney::Explain('ten');          // -> 'letters in the amount'

Methods

Amount

Read an amount from anything.

static function Amount(
    mixed $input = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    float $fallback = 0.0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): float
Parameter Type Default What it does
input mixed null The value: a number, or money text.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point. Comma declares the European mirror: dots group, the comma decides.
fallback float 0.0 What comes back when the value cannot be read.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when the value cannot be read.

Returns

When Default With throwIfInvalid: true
The value reads as an amount The amount, the nearest float: 10.99 The amount: 10.99
Rejected: missing, letters in the amount, or grouping that does not match the declared mark The fallback (default 0.0) Throws CkMoneyException — carries Input, Expected and Reason

A 0.0 answer can also be a real zero amount — set a distinctive fallback when you need to tell them apart.

Takes a number, or text in the shapes real data comes in: plain '10.99', grouped '$1,234.56', the accounting negatives '(123.45)' and '123.45-'. The symbols $, €, £, ¥ are ignored, and a known ISO 4217 code at either end ('USD 10.99', '10.99 EUR') is dropped. The amount comes back exactly as read — no rounding to any currency's decimals. Text naming two different codes, 'USD 10.99 EUR', is rejected.

The reading is exact, on every digit; only the answer is a float, the nearest one to the amount, since PHP has no decimal type. A float cannot remember that 10 was written '10.00' — only text can — so for the amount with its currency's decimals, to save or to send, use Format; for whole cents, MinorUnits; for arithmetic, ToMoney.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Amount('USD 10.99');     // -> 10.99
CkMoney::Amount('EUR 1,234.56');  // -> 1234.56
CkMoney::Amount('(123.45)');      // -> -123.45  (accounting negative)
CkMoney::Amount('123.45-');       // -> -123.45  (trailing minus)

By default a dot is the decimal point and commas group digits, as in the US. For European text ('1.234,56') you declare the mirror with the DecimalMark enum — it is never guessed, because '1.234' means two different numbers in the two notations:

use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\Enums\DecimalMark;

CkMoney::Amount(
    input: '1.234,56',
    mark: DecimalMark::Comma
);   // -> 1234.56

CkMoney::Amount('1,23');          // -> 0.0  (comma decimal not declared — rejected, never misread)

A fallback replaces an unreadable value:

use SafeCoder\CommonKit\CkMoney;

CkMoney::Amount(
    input: 'junk',
    fallback: -1.0
);   // -> -1.0

AmountOrNull

Null when there is nothing.

static function AmountOrNull(
    mixed $input = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $treatZeroAsMissing = true
): ?float
Parameter Type Default What it does
input mixed null The value: a number, or money text.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal mark in text.
treatZeroAsMissing bool true Read a zero amount as missing, so it answers null too; pass false to keep a zero as the real amount it sometimes is.

Returns

When You get
The value reads as an amount worth keeping The amount: 1234.5
Missing (null), unreadable, or a zero with treatZeroAsMissing on null — this method never throws

Reads exactly like Amount, but answers null instead of a fallback when there is nothing usable: a missing value, unreadable text — or a zero, because in many database columns 0.00 really means "no amount". That last part is on by default and switched off with treatZeroAsMissing: false. Built for filling nullable columns honestly.

use SafeCoder\CommonKit\CkMoney;

CkMoney::AmountOrNull('$1,234.50');   // -> 1234.5
CkMoney::AmountOrNull('junk');        // -> null
CkMoney::AmountOrNull('0.00');        // -> null  (zero counts as missing by default)

CkMoney::AmountOrNull(
    input: '0.00',
    treatZeroAsMissing: false
);   // -> 0.0  (a real zero, kept on request)

A processor avoids decimal points entirely: it charges in minor units, a currency's smallest coin, as a whole number. US$10.99 is 1099 cents. The next two methods convert to and from that integer.

How many decimals a currency has comes from the built-in ISO 4217 table — and it is not always two. The Japanese yen has no smaller coin at all, so ¥1099 is 1099 minor units; the Bahraini dinar has three decimals, so 3.141 dinars is 3141.

MinorUnits

An amount into integer cents.

static function MinorUnits(
    mixed $input = null,
    ?string $currency = 'USD',
    ?int $decimals = null,
    ?int $scale = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    int $fallback = 0,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
Parameter Type Default What it does
input mixed null The amount: a number, or text.
currency ?string 'USD' The ISO 4217 code deciding the scale — how many decimals make one minor unit. Money text that names another currency is refused.
decimals ?int null How many decimals the amount may carry, in place of the currency's own — for a receiver that disagrees with ISO 4217, or a unit price kept at four. 0 to CkSettings::MaxMoneyDecimals (8); null for the currency's own.
scale ?int null The power of ten the whole units are written at, when it is not the decimals — a receiver that writes a currency without decimals as if it had two (5 ISK as 500). null for the decimals.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
fallback int 0 What comes back when a value cannot be used.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The amount and the currency both read The minor units: 1099 The minor units: 1099
Rejected: an unknown currency, an unreadable amount, text naming another currency, or a count outside the range of minor units The fallback (default 0) Throws CkMoneyException — carries Input, Expected and Reason

More decimals than the currency has are rounded away from zero before converting.

MinorUnits converts an amount to that integer of minor units.

Most receivers count a currency's decimals the way ISO 4217 does, and the defaults are right. A few do not, and they say so in their documentation: one writes a currency that has no decimals as if it had two, so 5 ISK goes out as 500. Say it with scale, and the amount is still rounded to whole króna first. Your processor's own documentation says which currencies it counts differently.

use SafeCoder\CommonKit\CkMoney;

CkMoney::MinorUnits(
    input: 5,
    currency: 'ISK',
    scale: 2
);   // -> 500  (no decimals, written as two)
use SafeCoder\CommonKit\CkMoney;

CkMoney::MinorUnits('$10.99');    // -> 1099  (USD is the default currency)

CkMoney::MinorUnits(
    input: '£99.50',
    currency: 'GBP'
);   // -> 9950  (pence)

CkMoney::MinorUnits(
    input: 1099,
    currency: 'JPY'
);   // -> 1099  (the yen has no decimals)

CkMoney::MinorUnits('10.005');    // -> 1001  (extra decimals round away from zero first)

CkMoney::MinorUnits('1099 JPY');  // -> 0  (the text says JPY, the currency asked for is USD)

FromMinorUnits

Integer cents back into an amount.

static function FromMinorUnits(
    mixed $input = null,
    ?string $currency = 'USD',
    ?int $decimals = null,
    ?int $scale = null,
    string $fallback = '0',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The minor units: a whole number, or text holding one.
currency ?string 'USD' The ISO 4217 code deciding the scale.
decimals ?int null How many decimals the amount may carry, in place of the currency's own — for a receiver that disagrees with ISO 4217, or a unit price kept at four. 0 to CkSettings::MaxMoneyDecimals (8); null for the currency's own.
scale ?int null The power of ten the whole units are written at, when it is not the decimals — a receiver that writes a currency without decimals as if it had two (5 ISK as 500). null for the decimals.
fallback string '0' What comes back when a value cannot be used, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The count and the currency both read The amount, as text with the currency's decimals: '10.99' The amount: '10.99'
Rejected: an unknown currency, or a count that is not a whole number The fallback (default '0') Throws CkMoneyException — carries Input, Expected and Reason

Minor units are whole by definition — a fractional count rejects rather than rounds.

Works like MinorUnits in reverse: it converts a count of minor units back into an amount, written as text with exactly the currency's decimals — exact, and ready for a DECIMAL column. Minor units are whole by definition, so FromMinorUnits rejects a fractional count like 1099.5 — that is bad data, and you get the fallback.

With scale, a count written at a finer scale than the currency's decimals reads back: 500 at scale 2 is 5 ISK. A count that would not be whole — 550 — is rejected as bad data.

use SafeCoder\CommonKit\CkMoney;

CkMoney::FromMinorUnits(1099);    // -> '10.99'

CkMoney::FromMinorUnits(
    input: 3141,
    currency: 'BHD'
);   // -> '3.141'  (the dinar has three)

CkMoney::FromMinorUnits(
    input: 500,
    currency: 'ISK',
    scale: 2
);   // -> '5'  (written as two decimals, worth five króna)

CkMoney::FromMinorUnits(1099.5);  // -> '0'  (a fraction of a cent is bad data)

Currency

Validate an ISO 4217 code.

static function Currency(
    mixed $input = null,
    mixed $fallback = null,
    TextCase $textCase = TextCase::Uppercase,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The raw code.
fallback mixed null Backup code used when the value is rejected. It goes through the same cleaning and checking; a fallback that is itself rejected answers ''.
textCase TextCase TextCase::Uppercase The casing the code is written in: TextCase::Lowercase for a receiver that takes 'usd'; upper case, the ISO form, for any other value.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering '', when neither the value nor the fallback can be used.

Returns

When Default With throwIfInvalid: true
The value (or the fallback) is an ISO 4217 code the library knows The upper-case code: 'USD' The upper-case code: 'USD'
Rejected: missing, not three letters or three digits, or not a known code Empty string Throws CkMoneyException — carries Input, Expected and Reason

The table holds 160 codes: the active ones people are paid in, plus the two most recently withdrawn, ANG and BGN, which stored records still carry. A long-retired code (DEM, FRF, ROL) rejects like any other word, and so do the fund and accounting codes nobody is paid in (BOV, CHE, USN, …).

Cleans a currency code and checks it against the built-in ISO 4217 table.

use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\Enums\TextCase;

CkMoney::Currency('usd');         // -> 'USD'
CkMoney::Currency('eur');         // -> 'EUR'
CkMoney::Currency(
    input: 'USD',
    textCase: TextCase::Lowercase
);   // -> 'usd'
CkMoney::Currency('yen');         // -> ''  (an everyday word, not the ISO code — that's 'JPY')

CkMoney::Currency(
    input: 'xyz',
    fallback: 'USD'
);   // -> 'USD'

CurrencyOf

The currency a country uses.

static function CurrencyOf(
    ?string $country = null,
    ?string $fallback = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
country ?string null The country: an ISO 3166-1 code, two or three letters, or a name — read the way CkCountry::Alpha2 reads one.
fallback ?string null A currency code that replaces an empty result, once it has been through the same validation Currency gives one.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning an empty string, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The country reads and has a currency on record The upper-case code: 'RON' The upper-case code: 'RON'
Rejected: not a country, or a place with no currency on record, and no usable fallback Empty string Throws CkMoneyException — carries Input, Expected and Reason

For defaulting a form or a price list from the customer's country. Where a country has more than one legal tender the one in everyday use answers: Panama is PAB, El Salvador USD, Bhutan BTN, Haiti HTG, Lesotho LSL, Namibia NAD, Uruguay UYU, Venezuela VES — and where ISO 4217 lists none, the one in everyday use: Palestine ILS, South Georgia GBP. Antarctica has a country code and no currency, so it is rejected.

use SafeCoder\CommonKit\CkMoney;

CkMoney::CurrencyOf('ro');             // -> 'RON'
CkMoney::CurrencyOf('Germany');        // -> 'EUR'
CkMoney::CurrencyOf('JPN');            // -> 'JPY'
CkMoney::CurrencyOf('Atlantis');       // -> ''

CkMoney::CurrencyOf(
    country: 'Atlantis',
    fallback: 'usd'
);   // -> 'USD'

NumericCode

The three digits card networks carry.

static function NumericCode(
    ?string $currency = null,
    string $fallback = '',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
currency ?string null The ISO 4217 code: three letters in any casing, or three digits.
fallback string '' What comes back when the code cannot be used, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when the code cannot be used.

Returns

When Default With throwIfInvalid: true
The code is one the library knows Its numeric code, three digits: '978' Its numeric code, three digits: '978'
Rejected: missing, not three letters or three digits, or not a known code The fallback (default empty) Throws CkMoneyException — carries Input, Expected and Reason

A payment API speaks letters — 'eur', 'EUR'. A card network, an acquirer's settlement file or a bank's ISO 8583 message speaks digits: field 49 of an authorisation and EMV tag 5F2A carry 978 for the euro. NumericCode writes them, as text, because the zeros count: the Albanian lek is '008'.

The way back needs no method of its own. Currency, and the currency parameter of every method, read three digits as the letters they stand for — so a value taken straight from such a message works everywhere:

use SafeCoder\CommonKit\CkMoney;

CkMoney::NumericCode('eur');      // -> '978'
CkMoney::NumericCode('ALL');      // -> '008'  (three digits, the zeros kept)
CkMoney::NumericCode('lei');      // -> ''

CkMoney::Currency('978');         // -> 'EUR'

CkMoney::MinorUnits(
    input: '1099',
    currency: '392'
);   // -> 1099  (392 is JPY: no decimals)

ISO hands a number on when one currency takes another's place: ANG and the XCG that replaced it in 2025 are both '532', and '532' reads as XCG, the currency in use. Digits inside an amount are never a currency — '978 10.99' is a number, not ten euros.

Decimals

How many decimals a currency has.

static function Decimals(
    ?string $currency = null,
    int $fallback = -1,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
Parameter Type Default What it does
currency ?string null The ISO 4217 code, in any casing.
fallback int -1 What comes back when the code cannot be used — not 0, which is a real answer.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The code is one the library knows Its decimals, 0 to 4: 2 Its decimals, 0 to 4: 2
Rejected: missing, not three letters or three digits, or not a known code The fallback (default -1) Throws CkMoneyException — carries Input, Expected and Reason

What a form needs for its input step, and a message for its wording ("at most two decimals"). It is the number Format writes with and MinorUnits scales by.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Decimals('usd');   // -> 2
CkMoney::Decimals('JPY');   // -> 0   (the yen has no smaller coin)
CkMoney::Decimals('BHD');   // -> 3
CkMoney::Decimals('lei');   // -> -1  (a word, not a code)

Format

Write it the way an API expects.

static function Format(
    mixed $input = null,
    ?string $currency = 'USD',
    ?int $decimals = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    mixed $fallback = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The amount: a number, or text.
currency ?string 'USD' The ISO 4217 code deciding the decimals. Money text that names another currency is refused.
decimals ?int null How many decimals the amount may carry, in place of the currency's own — for a receiver that disagrees with ISO 4217, or a unit price kept at four. 0 to CkSettings::MaxMoneyDecimals (8); null for the currency's own.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
fallback mixed null An amount to use when the input cannot be read, put through the same reading and formatting; a fallback that is itself rejected answers ''.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering '', when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The amount and the currency both read The formatted text: '10.00' The formatted text: '10.00'
Rejected: an unknown currency, or an unreadable amount with no usable fallback Empty string Throws CkMoneyException — carries Input, Expected and Reason

The fallback stands in for an unreadable amount only — an unknown currency always answers empty.

Writes an amount with exactly its currency's decimals: plain digits, a dot, no grouping, no symbol — the shape a payment API wants ('10.00', never '10'), and the shape a DECIMAL column travels in. Extra decimals are rounded away from zero first. This is the text every other method here reads back, so what Format writes today, Amount, MinorUnits and Total read tomorrow.

With decimals, the text has that many decimals in place of the currency's own — 0 for a receiver that refuses a decimal forint amount, 4 for a unit price.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Format(
    input: '1000.50',
    currency: 'HUF',
    decimals: 0
);   // -> '1001'
use SafeCoder\CommonKit\CkMoney;

CkMoney::Format(10);              // -> '10.00'  (USD is the default)

CkMoney::Format(
    input: '99.5',
    currency: 'GBP'
);   // -> '99.50'

CkMoney::Format(
    input: 1099,
    currency: 'JPY'
);   // -> '1099'  (no decimals at all)

CkMoney::Format(
    input: 3.1,
    currency: 'BHD'
);   // -> '3.100'

CkMoney::Format('junk');          // -> ''  (unreadable, and no fallback given)

Display

Write it the way a person reads it.

static function Display(
    mixed $input = null,
    ?string $currency = 'USD',
    DecimalMark $into = CkSettings::DefaultDecimalMark,
    bool $showCurrency = true,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    string $fallback = '',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The amount: a number, or text.
currency ?string 'USD' The ISO 4217 code whose decimals apply. Money text that names another currency is refused.
into DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in the text written; the other one groups the digits.
showCurrency bool true Write the currency code after the amount.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in the text read.
fallback string '' What comes back when a value cannot be used, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The amount and the currency both read The text: '1,234.50 EUR' The text: '1,234.50 EUR'
Rejected: an unknown currency, or an unreadable amount The fallback (default empty) Throws CkMoneyException — carries Input, Expected and Reason

Format is the shape for machines; this is the one for a screen, an e-mail, a PDF: digits grouped in threes, exactly the currency's decimals, the code after it. The code and not a symbol, on purpose — $ is a dozen currencies, and a locale's formatter takes its symbol and its separators from the locale, so the same amount prints differently on every server. No locale is consulted here. What Display writes, Amount reads back under the same mark. For a locale's own conventions ask the Money object instead: CkMoney::ToMoney($input)->formatToLocale('ro_RO').

use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\Enums\DecimalMark;

CkMoney::Display(
    input: 1234.5,
    currency: 'EUR'
);   // -> '1,234.50 EUR'

CkMoney::Display(
    input: 1099,
    currency: 'JPY'
);   // -> '1,099 JPY'

CkMoney::Display(
    input: 1234.5,
    currency: 'EUR',
    into: DecimalMark::Comma
);   // -> '1.234,50 EUR'

CkMoney::Display(
    input: '(1234.5)',
    showCurrency: false
);   // -> '-1,234.50'

Round

To the currency's decimals, away from zero.

static function Round(
    mixed $input = null,
    ?string $currency = 'USD',
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    string $fallback = '0',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The amount: a number, or text.
currency ?string 'USD' The ISO 4217 code deciding the decimals. Money text that names another currency is refused.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
fallback string '0' What comes back when a value cannot be used, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The amount and the currency both read The rounded amount, as text with the currency's decimals: '10.01' The rounded amount: '10.01'
Rejected: an unknown currency, or an unreadable amount The fallback (default '0') Throws CkMoneyException — carries Input, Expected and Reason

Rounds by CkSettings::MoneyRounding (MidpointRounding::AwayFromZero) — the same rule every CkMoney conversion uses.

Rounds an amount to its currency's decimals the accounting way: a midpoint moves away from zero, so US$10.005 becomes 10.01. (Halves to the even neighbour — the rule CkNumber::Round applies by default — would make that 10.00: good for statistics, surprising on an invoice.) Format writes the same digits; the two differ in what they hand back for a value they cannot use — empty there, the fallback here.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Round(10.005);           // -> '10.01'  (USD is the default)

CkMoney::Round(
    input: '99.999',
    currency: 'GBP'
);   // -> '100.00'

CkMoney::Round(
    input: 10.5,
    currency: 'JPY'
);   // -> '11'

Split

Divide without losing a cent.

static function Split(
    mixed $input = null,
    int $parts = 2,
    ?string $currency = 'USD',
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): array
Parameter Type Default What it does
input mixed null The amount: a number, or text.
parts int 2 How many parts, 1 to CkSettings::MaxMoneySplitParts (1000).
currency ?string 'USD' The ISO 4217 code deciding the minor unit. Money text that names another currency is refused.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering an empty array, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The amount, the currency and the count all read The parts, in order, each as text with the currency's decimals: ['33.34', '33.33', '33.33'] The parts, in order: ['33.34', '33.33', '33.33']
Rejected: an unreadable amount, an unknown currency, or a part count out of range Empty array — never null Throws CkMoneyException — carries Input, Expected and Reason

The amount is first rounded to the currency's decimals — parts can only differ by whole minor units.

Divides an amount into parts (2 by default, up to 1000) that always sum back to exactly the whole. Naive division of 100 by 3 gives three 33.33s and a lost cent; Split hands the extra minor units to the earlier parts instead.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Split(
    input: 100,
    parts: 3
);   // -> ['33.34', '33.33', '33.33']  (sums back to exactly 100.00)

CkMoney::Split(
    input: '£10',
    parts: 3,
    currency: 'GBP'
);   // -> ['3.34', '3.33', '3.33']

CkMoney::Split(
    input: -10,
    parts: 3
);   // -> ['-3.34', '-3.33', '-3.33']  (negatives split the same way)

CkMoney::Split('junk');           // -> []

Allocate

Split by ratio, never losing a cent.

static function Allocate(
    mixed $input = null,
    mixed $ratios = null,
    ?string $currency = 'USD',
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): array
Parameter Type Default What it does
input mixed null The amount: a number, or text.
ratios mixed null The ratios: a list of numbers, none below zero and at least one above, up to CkSettings::MaxMoneySplitParts (1000). Only their proportions matter — 70 and 30, 7 and 3, 0.7 and 0.3 are one split. An array with keys is the list of its values.
currency ?string 'USD' The ISO 4217 code whose decimals apply. Money text that names another currency is refused.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning an empty array, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
Everything reads The parts, in the order of the ratios, each as text with the currency's decimals: ['70.01', '30.00'] The parts, in the order of the ratios: ['70.01', '30.00']
Rejected: an unreadable amount or ratio, a ratio below zero, no ratios, all zeros, an unknown currency An empty array Throws CkMoneyException — carries Input, Expected and Reason

A revenue share, a discount spread over invoice lines by their value, a payment divided between accounts. Doing it with Multiply loses or invents cents — half of US$0.05, twice, is 0.03 + 0.03. Here each part gets its proportional share rounded down to the minor unit, and the units left over go one at a time to the parts that lost the most in the rounding (the largest-remainder method; a tie goes to the earlier part). A part whose ratio is zero gets nothing. Split is the same with equal ratios.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Allocate(
    input: 100.01,
    ratios: [70, 30]
);   // -> ['70.01', '30.00']

CkMoney::Allocate(
    input: '$1.00',
    ratios: [2, 3, 1]
);   // -> ['0.33', '0.50', '0.17']

CkMoney::Allocate(
    input: 0.05,
    ratios: [1, 1]
);   // -> ['0.03', '0.02']  (never two 0.03s)

CkMoney::Allocate(
    input: 1.00,
    ratios: [0, 2, 1]
);   // -> ['0.00', '0.67', '0.33']

// a 25.00 discount over three invoice lines, by their value
CkMoney::Allocate(
    input: '25.00',
    ratios: [199.99, 49.50, 12.00]
);   // -> ['19.12', '4.73', '1.15']  (sums back to exactly 25.00)

IsAmount

Does it read as money at all?

static function IsAmount(
    mixed $input = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when the value is not an amount.

Returns

When Default With throwIfInvalid: true
A number, or money text: '10.99', '$1,234.56', '(123.45)', 'USD 10.99' true true
Rejected: not an amount — missing, letters, an undeclared decimal comma, an unknown code false Throws CkMoneyException — carries Input, Expected and Reason

The yes-or-no form of Amount. Zero is an amount here, whatever AmountOrNull says about it; when the value is going to be stored, read it with that method instead and check for null.

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsAmount('$1,234.56');   // -> true
CkMoney::IsAmount('1,23');        // -> false  (a decimal comma needs declaring)
CkMoney::IsAmount('1e5');         // -> false  (letters are junk in money)

IsCurrency

A known ISO 4217 code?

static function IsCurrency(
    mixed $input = null,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The raw code.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when the value is not a known code.

Returns

When Default With throwIfInvalid: true
Three letters in any casing, or three digits, that name a known code true true
Rejected: not three letters or three digits, or not a known code false Throws CkMoneyException — carries Input, Expected and Reason

The yes-or-no form of Currency; call that instead when you want the upper-case spelling back.

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsCurrency('usd');   // -> true
CkMoney::IsCurrency('US$');   // -> false

InCurrencies

One of the currencies we settle in?

static function InCurrencies(
    mixed $input = null,
    mixed ...$codes
): bool
Parameter Type Default What it does
input mixed null The raw code.
codes mixed ... — The codes to accept, in any casing — each argument a code or a list of codes. Entries that are not known codes are skipped.

Returns

When You get
The value is a known code and an entry names the same one true
It is not a known code, no entry matches, or the list is empty false — an answer, not a failure; it never throws

The value and every entry are read the way Currency reads them. A variadic list must be last, so there is no throwIfInvalid; call Currency with it first when you need the exception.

use SafeCoder\CommonKit\CkMoney;

CkMoney::InCurrencies(
    input: 'usd',
    codes: ['USD', 'EUR', 'GBP']
);   // -> true

CkMoney::InCurrencies(
    input: 'JPY',
    codes: ['USD', 'EUR', 'GBP']
);   // -> false

IsRounded

Already at the currency's precision?

static function IsRounded(
    mixed $input = null,
    ?string $currency = 'USD',
    ?int $decimals = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
currency ?string 'USD' The ISO 4217 code whose decimals apply. Money text that names another currency is refused.
decimals ?int null How many decimals the amount may carry, in place of the currency's own — for a receiver that disagrees with ISO 4217, or a unit price kept at four. 0 to CkSettings::MaxMoneyDecimals (8); null for the currency's own.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when the value is not an amount or the currency is not a known code.

Returns

When Default With throwIfInvalid: true
The amount carries no more decimals than the currency allows true true
It carries more false false
Rejected: not an amount, or an unknown currency false Throws CkMoneyException — carries Input, Expected and Reason

The pre-charge check: a processor rejects an amount it cannot express in minor units. 10.99 US dollars yes, 10.999 no; 1099 yen yes, 1099.5 no; 3.141 Bahraini dinars yes. Trailing zeros are not extra precision, so '10.990' passes. The amount Format and ToMoney would not have to round.

With decimals, the check is against that many: '1000.50' in HUF with decimals: 0 is false — it fits the forint, and not a receiver that takes whole forint only.

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsRounded('10.99');            // -> true   (USD by default)
CkMoney::IsRounded('10.999');           // -> false
CkMoney::IsRounded(
    input: '1099.5',
    currency: 'JPY'
);   // -> false

IsPositive

Above zero?

static function IsPositive(
    mixed $input = null,
    bool $allowZero = false,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
allowZero bool false Count zero as well.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when the value is not an amount.

Returns

When Default With throwIfInvalid: true
An amount above zero — or zero itself with allowZero true true
Zero (by default), or a negative amount, '(123.45)' included false false
Rejected: not an amount — missing, letters, an undeclared decimal comma, an unknown code false Throws CkMoneyException — carries Input, Expected and Reason

"A payment must be positive"; with allowZero: true, "a balance must not be negative". CkNumber::IsPositive refuses '$5', which is why this one exists.

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsPositive('$5');        // -> true
CkMoney::IsPositive('$0.00');     // -> false
CkMoney::IsPositive(
    input: '$0.00',
    allowZero: true
);   // -> true

IsNegative

Below zero?

static function IsNegative(
    mixed $input = null,
    bool $allowZero = false,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
allowZero bool false Count zero as well.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when the value is not an amount.

Returns

When Default With throwIfInvalid: true
An amount below zero — '(123.45)', '123.45-', '-$5' — or zero itself with allowZero true true
Zero (by default), or a positive amount false false
Rejected: not an amount — missing, letters, an undeclared decimal comma, an unknown code false Throws CkMoneyException — carries Input, Expected and Reason

"A refund must be negative", "a credit note". The mirror of IsPositive; IsNegative, IsZero and IsPositive partition the amounts.

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsNegative('(123.45)');   // -> true
CkMoney::IsNegative('$5');         // -> false

IsZero

Exactly zero?

static function IsZero(
    mixed $input = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when the value is not an amount.

Returns

When Default With throwIfInvalid: true
An amount equal to zero: 0, '0.00', '$0', '(0.00)' true true
Any other amount false false
Rejected: not an amount — missing, letters, an undeclared decimal comma, an unknown code false Throws CkMoneyException — carries Input, Expected and Reason

AmountOrNull calls a zero amount "missing" by default; this says so out loud, for data-quality checks and for telling "nothing charged" from "not an amount".

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsZero('$0.00');   // -> true
CkMoney::IsZero('junk');    // -> false  (not an amount, not zero)

IsBetween

Inside a range?

static function IsBetween(
    mixed $input = null,
    mixed $min = null,
    mixed $max = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
min mixed null The lower bound, inclusive, read the same way — '$10.00' works. null means no lower bound.
max mixed null The upper bound, inclusive, read the same way. null means no upper bound.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says which value is not an amount and why — or that the bounds contradict each other — instead of answering false.

Returns

When Default With throwIfInvalid: true
The value and the given bounds read, and the value lies inside, both ends included true true
Everything reads, and the value lies outside false false
Rejected: an unreadable value or bound, bounds that contradict each other, or values naming different currencies false Throws CkMoneyException — carries Input, Expected and Reason

"Minimum order", "transfer limit". Bounds are read as amounts too, and the mark applies to all three; a null bound is open, so IsBetween($total, 10, null) is "at least ten".

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsBetween(
    input: '$50.00',
    min: '$10.00',
    max: '$10,000.00'
);   // -> true

CkMoney::IsBetween(
    input: '9.99',
    min: 10,
    max: 100
);   // -> false

CkMoney::IsBetween(
    input: '50',
    min: 100,
    max: 10
);   // -> false  (contradictory bounds)

IsAtLeast

At least this much?

static function IsAtLeast(
    mixed $input = null,
    mixed $min = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
min mixed null The lower bound, inclusive, read the same way.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when either value is not an amount.

Returns

When Default With throwIfInvalid: true
Both read, and the value is greater than or equal to min true true
Both read, and the value is below min false false
Rejected: either is not an amount, or the two name different currencies false Throws CkMoneyException — carries Input, Expected and Reason

"A minimum order of $10", "a deposit of at least 100". The bound counts.

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsAtLeast(
    input: '$10.01',
    min: '$10.00'
);   // -> true

CkMoney::IsAtLeast(
    input: '9.99',
    min: 10
);   // -> false

IsAtMost

At most this much?

static function IsAtMost(
    mixed $input = null,
    mixed $max = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
max mixed null The upper bound, inclusive, read the same way.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when either value is not an amount.

Returns

When Default With throwIfInvalid: true
Both read, and the value is less than or equal to max true true
Both read, and the value is above max false false
Rejected: either is not an amount, or the two name different currencies false Throws CkMoneyException — carries Input, Expected and Reason

"A transfer limit of $10,000", "a refund no larger than the payment". The bound counts.

use SafeCoder\CommonKit\CkMoney;

CkMoney::IsAtMost(
    input: '9,999.99',
    max: '$10,000'
);   // -> true

CkMoney::IsAtMost(
    input: '10,000.01',
    max: 10000
);   // -> false

SameAmount

The same sum, whatever the dressing?

static function SameAmount(
    mixed $a = null,
    mixed $b = null,
    float $tolerance = 0.0,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
a mixed null The first value: a number, or money text.
b mixed null The second value, read the same way.
tolerance float 0.0 How far apart the two may be and still count as the same. Below 0 acts as 0.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says which value is not an amount and why, instead of answering false.

Returns

When Default With throwIfInvalid: true
Both read as amounts and differ by at most tolerance true true
Both read, and differ by more — or name different currencies false false
Rejected: either is not an amount false Throws CkMoneyException — carries Input, Expected and Reason

The reconciliation check — "the payment equals the invoice" — comparing the amounts. Pass a tolerance of 0.01 for feeds that round differently. Two values that name different currencies, '50 EUR' and '50 USD', are never the same amount; a value that names none goes with any. Two values that are not amounts are never the same either.

use SafeCoder\CommonKit\CkMoney;

CkMoney::SameAmount(
    a: '$1,234.56',
    b: 1234.56
);   // -> true

CkMoney::SameAmount(
    a: '10.99',
    b: '10.98'
);   // -> false

CkMoney::SameAmount(
    a: '10.99',
    b: '10.98',
    tolerance: 0.01
);   // -> true

HasCurrency

Does the text say which currency?

static function HasCurrency(
    mixed $input = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
Parameter Type Default What it does
input mixed null The value: a number, or money text in any shape Amount reads.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering false, when the value is not an amount.

Returns

When Default With throwIfInvalid: true
An amount whose text carries a known code at either end or one of the symbols $ € £ ¥ true true
A bare amount, or a number, which declares nothing false false
Rejected: not an amount — missing, letters, an undeclared decimal comma, an unknown code false Throws CkMoneyException — carries Input, Expected and Reason

For multi-currency intake, where a bare '10.99' is ambiguous and should be refused. It says that a currency was declared, not which: $ alone cannot tell US, Canadian and Australian dollars apart. The accounting wrapper does not hide the code, so '(USD 10.99)' declares one.

use SafeCoder\CommonKit\CkMoney;

CkMoney::HasCurrency('USD 10.99');   // -> true
CkMoney::HasCurrency('$10.99');      // -> true
CkMoney::HasCurrency('10.99');       // -> false

Multiply

Tax, discounts, rounded the accounting way.

static function Multiply(
    mixed $input = null,
    mixed $factor = null,
    ?string $currency = 'USD',
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    string $fallback = '0',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The amount: a number, or text.
factor mixed null The multiplier, a rate such as 1.0825, not a percentage — a number, or text that reads as one.
currency ?string 'USD' The ISO 4217 code whose decimals apply. Money text that names another currency is refused.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in the amount.
fallback string '0' What comes back when a value cannot be read, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The amount, the factor and the currency read The product, rounded to the currency's decimals away from zero, as text: '21.64' The product: '21.64'
Rejected: any of them unusable The fallback (default '0') Throws CkMoneyException — carries Input, Expected and Reason — Expected names which

The multiplication that goes wrong when it is done on floats or rounded by halves-to-even. The product is exact and rounded once, as Round rounds — and the answer is text, so nothing invites * and +, which is where 0.1 + 0.2 stops being 0.3. The Money object gives the same digits: CkMoney::ToMoney($input)->multipliedBy($factor, RoundingMode::HalfUp).

use SafeCoder\CommonKit\CkMoney;

CkMoney::Multiply(
    input: '$19.99',
    factor: 1.0825
);   // -> '21.64'  (the price with 8.25 percent tax)

CkMoney::Multiply(
    input: 100,
    factor: 0.9
);   // -> '90.00'  (a ten-percent discount)

Divide

An amount by a number, rounded once.

static function Divide(
    mixed $input = null,
    mixed $divisor = null,
    ?string $currency = 'USD',
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    string $fallback = '0',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The amount: a number, or text.
divisor mixed null The divisor: a number, or text that reads as one; not zero. A plain number, not money.
currency ?string 'USD' The ISO 4217 code whose decimals apply. Money text that names another currency is refused.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
fallback string '0' What comes back when a value cannot be used, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
Everything reads The quotient, rounded to the currency, as text: '33.33' The quotient, rounded to the currency: '33.33'
Rejected: division by zero, an unreadable amount or divisor, an unknown currency The fallback (default '0') Throws CkMoneyException — carries Input, Expected and Reason

A unit price from a total and a quantity; a monthly figure from a yearly one. Multiply cannot do it exactly — a third has no exact decimal. To share an amount out so that the parts add back to the whole, use Split or Allocate: three 33.33s are 99.99.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Divide(
    input: '$100',
    divisor: 3
);   // -> '33.33'

CkMoney::Divide(
    input: 1099,
    divisor: 2,
    currency: 'JPY'
);   // -> '550'  (whole yen)

CkMoney::Divide(
    input: '10',
    divisor: 0
);   // -> '0'  (division by zero: the fallback)

Convert

To another currency, at your rate.

static function Convert(
    mixed $input = null,
    mixed $rate = null,
    ?string $from = null,
    ?string $to = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    string $fallback = '0',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The amount: a number, or text. Money text that names a currency must name from.
rate mixed null The exchange rate, above zero: how many units of to one unit of from buys.
from ?string null The ISO 4217 code the amount is in.
to ?string null The ISO 4217 code to convert it to; its decimals decide the rounding.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
fallback string '0' What comes back when a value cannot be used, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
Everything reads The converted amount, rounded to to, as text: '92.00' The converted amount, rounded to to: '92.00'
Rejected: an unknown currency, a rate that is not above zero, an unreadable amount, text naming another currency than from The fallback (default '0') Throws CkMoneyException — carries Input, Expected and Reason

The library holds no rates: where they come from, and how fresh they are, is yours to decide. What it adds is the rounding for the target currency — dollars into yen lose their decimals — and the check that a value saying '100 EUR' is not converted from dollars.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Convert(
    input: '$100',
    rate: 0.92,
    from: 'USD',
    to: 'EUR'
);   // -> '92.00'

CkMoney::Convert(
    input: 100,
    rate: 157.35,
    from: 'USD',
    to: 'JPY'
);   // -> '15735'  (the yen has no decimals)

CkMoney::Convert(
    input: '100 EUR',
    rate: 0.92,
    from: 'USD',
    to: 'EUR'
);   // -> '0'  (the text says EUR, not USD)

Subtract

One amount less another.

static function Subtract(
    mixed $input = null,
    mixed $amount = null,
    ?string $currency = 'USD',
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    string $fallback = '0',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
input mixed null The amount to take from: a number, or text.
amount mixed null The amount to take away: a number, or text — money-dressed like the first.
currency ?string 'USD' The ISO 4217 code whose decimals apply. Money text that names another currency is refused, in either value.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
fallback string '0' What comes back when a value cannot be used, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning the fallback, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
Both amounts and the currency read The difference, rounded to the currency, as text: '74.50' The difference, rounded to the currency: '74.50'
Rejected: an unreadable amount, an unknown currency, text naming another currency The fallback (default '0') Throws CkMoneyException — carries Input, Expected and Reason

A total less a discount, a charge less a refund. Both values are read as money, so either may carry a symbol or grouping; the difference is exact and rounded once. It goes below zero when the second is the larger. To add, use Total.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Subtract(
    input: '$100.00',
    amount: '$25.50'
);   // -> '74.50'

CkMoney::Subtract(
    input: 10,
    amount: 25
);   // -> '-15.00'

CkMoney::Subtract(
    input: '100 USD',
    amount: '25 EUR'
);   // -> '0'  (the second value names another currency)

Total

A column of money, summed exactly.

static function Total(
    mixed $values = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    string $fallback = '0',
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
Parameter Type Default What it does
values mixed null An array of numbers or money text, or anything that can be looped over. An array with keys is the list of its values.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
fallback string '0' What comes back when the list is missing or an entry cannot be read, as given.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of answering the fallback, when the list or an entry cannot be used.

Returns

When Default With throwIfInvalid: true
Every entry reads The exact sum, as text without trailing zeros: '1232.55', '15'; an empty list totals '0' The sum
Rejected: no list, an entry that is not an amount, or entries naming different currencies The fallback (default '0') Throws CkMoneyException — carries Input, Expected and Reason — the reason names the entry

Strict on purpose: one entry that is not an amount rejects the whole total, since a sum that silently skipped a row is worse than none. A string is one value, not a list. No currency is asked for, so none is checked and nothing is rounded: the answer is the exact sum, written without trailing zeros — hand it to Round or Format for a currency's decimals. Entries that name different currencies, '10 USD' and '5 EUR', are not one total.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Total(['$1,234.56', '(12.00)', '9.99']);   // -> '1232.55'
CkMoney::Total(['10', '5']);                        // -> '15'  (no decimals invented)
CkMoney::Total([]);                                 // -> '0'
CkMoney::Total(['10 USD', '5 EUR']);                // -> '0'  (not one total: the fallback)

ToMoney

The amount and its currency as one Money object.

static function ToMoney(
    mixed $input = null,
    ?string $currency = 'USD',
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Money
Parameter Type Default What it does
input mixed null The amount: a number, or text.
currency ?string 'USD' The ISO 4217 code. Money text that names another currency is refused.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
throwIfInvalid bool CkSettings::DefaultThrowIfInvalid (false) Pass true to throw a CkMoneyException that says why, instead of returning a zero, when a value cannot be used.

Returns

When Default With throwIfInvalid: true
The amount and the currency both read The Money: Money USD 10.99, rounded to the currency's decimals The Money: Money USD 10.99
Rejected: an unreadable amount Zero in the currency asked for: Money USD 0.00 Throws CkMoneyException — carries Input, Expected and Reason
Rejected: an unknown currency Zero US dollars — ten of an unknown currency is never ten dollars Throws CkMoneyException

For an application that carries money around as a value and works on it: plus, minus, multipliedBy, allocate, isGreaterThan, formatToLocale. The amount is read the way Amount reads one, the currency the way Currency reads one, and the amount goes in exactly, never through a float. A Money is SafeCoder\CommonKit\Packages\Brick\Money\Money, the library's own copy of brick/money — not the Money of a brick/money the application installs itself; the two never meet, in whatever versions. Every calculation on this page gives the same digits as the Money object's own method, so start with the method here and move to the object when one call is not enough.

There is always a Money to work with, so a zero can be a rejected value as well as a real one; to tell them apart use ToMoneyOrNull, IsAmount or throwIfInvalid. From here on the arithmetic is the object's, and it is strict where this class is forgiving — it throws on a programmer's mistake: plus and minus refuse another currency, and multipliedBy and dividedBy refuse an answer that does not fit the currency's decimals unless told how to round, so pass the mode — RoundingMode::HalfUp, from SafeCoder\CommonKit\Packages\Brick\Math, is this library's CkSettings::MoneyRounding, away from zero.

use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\Enums\DecimalMark;
use SafeCoder\CommonKit\Packages\Brick\Math\RoundingMode;

CkMoney::ToMoney('$1,234.56');    // -> Money USD 1234.56  (USD is the default)

CkMoney::ToMoney(
    input: '99.5',
    currency: 'gbp'
);   // -> Money GBP 99.50

CkMoney::ToMoney(
    input: '1.234,56',
    currency: 'EUR',
    mark: DecimalMark::Comma
);   // -> Money EUR 1234.56

CkMoney::ToMoney('10.005');       // -> Money USD 10.01  (rounded to the currency's decimals)
CkMoney::ToMoney('junk');         // -> Money USD 0.00  (zero in the currency asked for)

$price = CkMoney::ToMoney('$19.99');
$price->plus(CkMoney::ToMoney('5.01'));                        // -> Money USD 25.00
$price->multipliedBy('1.0825', RoundingMode::HalfUp);          // -> Money USD 21.64
$price->isGreaterThan(CkMoney::ToMoney('10'));                 // -> true
(string) $price->getAmount();                                  // -> '19.99'

ToMoneyOrNull

Null when there is nothing.

static function ToMoneyOrNull(
    mixed $input = null,
    ?string $currency = 'USD',
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    bool $treatZeroAsMissing = true
): ?Money
Parameter Type Default What it does
input mixed null The amount: a number, or text.
currency ?string 'USD' The ISO 4217 code. Money text that names another currency is refused.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point in text.
treatZeroAsMissing bool true Read a zero amount as missing, so it answers null too; pass false to keep a zero as the real amount it sometimes is.

Returns

When You get
The amount and the currency both read, and the amount is worth keeping The Money: Money USD 1234.50
Missing (null), an unreadable amount or currency, or a zero with treatZeroAsMissing on null — this method never throws

Reads exactly like ToMoney, but answers null instead of a zero when there is nothing usable — the AmountOrNull of the Money object, for a nullable column read into a value.

use SafeCoder\CommonKit\CkMoney;

CkMoney::ToMoneyOrNull('$1,234.50');   // -> Money USD 1234.50
CkMoney::ToMoneyOrNull('junk');        // -> null
CkMoney::ToMoneyOrNull('0.00');        // -> null  (zero counts as missing by default)

CkMoney::ToMoneyOrNull(
    input: '10',
    currency: 'lei'
);   // -> null  (not a known currency)

CkMoney::ToMoneyOrNull(
    input: '0.00',
    treatZeroAsMissing: false
);   // -> Money USD 0.00  (a real zero, kept on request)

Explain

Why not — in words, without an exception.

static function Explain(
    mixed $input = null,
    DecimalMark $mark = CkSettings::DefaultDecimalMark,
    ?string $currency = null
): string
Parameter Type Default What it does
input mixed null The value: a number, or money text.
mark DecimalMark CkSettings::DefaultDecimalMark (DecimalMark::Dot) Which character is the decimal point.
currency ?string null The ISO 4217 code the amount is meant in. Given, the value is judged the way Format, MinorUnits and ToMoney judge it — known code, and no other currency named in the text. null judges the amount alone.

Returns

When You get
The value passes the reader ''
It does not The reason, the same words the exception would carry — an answer, not a failure; it never throws

The form-friendly twin of throwIfInvalid: the message to show beside the field, without a try and catch. Every class with a reader has one, on the reader's own rules, so the reason is exactly what the reader would say.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Explain('ten');        // -> 'letters in the amount'
CkMoney::Explain('1.234,56');   // -> 'a comma after the decimal point (declare DecimalMark.Comma to read European notation)'

CkMoney::Explain(
    input: '1099 JPY',
    currency: 'USD'
);   // -> 'the text says JPY, the currency asked for is USD'

Good to know

  • The answers come in four shapes, each exact. Amount and AmountOrNull answer a float — the nearest one to the amount, since PHP has no decimal type — for a value to show or compare. Format and every calculation answer numeric text with the currency's decimals, '21.64': exact, what a DECIMAL column arrives in and is saved from, read back by every method here, and nothing that invites * and +. MinorUnits answers an int. ToMoney answers the Money object, for a chain of arithmetic. Do the arithmetic of money with the methods here or on a Money — never with * and + on the floats Amount hands back.
  • A currency named in the text is checked, never just dropped. A code at either end of the text is dressing, and currency defaults to 'USD' — so MinorUnits('1099 JPY') would scale yen as dollars. Every method that is told a currency refuses a value whose text names another; pass currency: 'JPY' and it is 1099. The methods that compare or add several values — SameAmount, IsAtLeast, IsAtMost, IsBetween, Total — refuse two values that name different currencies: Total(['10 USD', '5 EUR']) is the fallback, not '15', and SameAmount('50 EUR', '50 USD') is false. A value that names no currency goes with any. The symbols $ € £ ¥ never count: each is shared by several currencies.
  • Decimal commas are declared, never guessed. '1.234,56' reads only with mark: DecimalMark::Comma; without it the value is rejected, and dot-decimal text is rejected under Comma — so an amount is never silently misread as a different number.
  • AmountOrNull and ToMoneyOrNull treat zero as missing by default. Pass treatZeroAsMissing: false when a zero is a real amount in your data.
  • Minor units use each currency's real ISO 4217 decimals — 2 for most, 0 for the yen family, 3 for the dinars. Never assume "times 100".
  • Round goes away from zero, not to the even neighbour — the accounting convention.
  • Split never loses a cent and the earlier parts carry the extras.
  • CkNumber will not read '$5' or '1,234' — money dressing is this class's job; plain numbers are CkNumber's.
  • Stray letters reject. Only known ISO codes are dropped from amount text, and '1e5' is not read as scientific notation — you get the fallback, not a surprise.
  • Everything is exact and locale-independent. Reading and calculating go digit by digit, with no ceiling on the digits, and the machine's locale is never consulted, so results are the same on every server. Pull raw values out of a payload bag with CkDictionary, then read them here.
  • The validators read money text and treat zero as an amount. IsPositive('$5') is the money spelling of CkNumber::IsPositive(5); IsZero('$0.00') is true even though AmountOrNull calls it missing. All of them answer false for anything unreadable, and every one but InCurrencies takes throwIfInvalid.
  • IsRounded is the pre-charge check. An amount with more decimals than its currency allows cannot become minor units without rounding; check it before MinorUnits, or let Round decide the rounding for you.
  • A Money is strict where this class is forgiving. The object ToMoney hands back throws on a programmer's mistake — mixing two currencies, a product that does not fit the currency's decimals with no rounding mode — where every method here answers a fallback.

If you want errors instead

Every method except AmountOrNull, ToMoneyOrNull and InCurrencies takes throwIfInvalid: true; a value that cannot be used then raises a CkMoneyException (from SafeCoder\CommonKit\Exceptions) instead of returning the fallback, '', false, the empty array or a zero Money. The exception carries the Input, what was Expected, and a short Reason, flattened into a one-line message. AmountOrNull and ToMoneyOrNull never throw — null is their answer for anything unusable; when you want errors, read the value with Amount or ToMoney and throwIfInvalid: true instead.

use SafeCoder\CommonKit\CkMoney;

CkMoney::Amount(
    input: 'ten dollars',
    throwIfInvalid: true
);   // throws CkMoneyException — the message says why