PHP Luminova: Convert, Format, and Split Money
Convert between major and minor units, format amounts, apply rates, and divide a total.
Use integer minor units when storing values such as cents. Use Money to convert amounts for display or apply common checkout calculations. Its calculation methods use PHP floating-point arithmetic and support rounding at the point shown in each method. Localized currency formatting requires the PHP Intl extension.
Usage
use Luminova\Utility\Money;
$cents = Money::toCents('19.95');
echo Money::formatCents($cents, prefix: '$'); // $19.95
$shares = Money::split('19.95', 2);
print_r($shares); // [9.97, 9.98]Class Definition
- Full namespace:
Luminova\Utility\Money - This class is final.
Methods
toCents
Multiply an amount by the divisor and round to the nearest integer.
public static function toCents(
string|float|int $amount,
int $divisor = 100
): intParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount in major units. |
$divisor | int | Number of minor units per major unit (default: 100). |
Return:
int - Amount in minor units.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric or the divisor is below one.
fromCents
Divide the integer minor-unit amount by the divisor and format with exactly two decimal places.
public static function fromCents(
string|int $cents,
int $divisor = 100
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$cents | string\|int | Amount in minor units, expressed as an integer or numeric string. |
$divisor | int | Number of minor units per major unit (default: 100). |
Return:
string - Amount in major units without thousands separators.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric or the divisor is below one.
format
Format an amount with a decimal point and comma thousands separators.
public static function format(
string|int|float $amount,
int $decimals = 2,
?string $prefix = null,
?string $suffix = null
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|int\|float | Numeric amount in major units. |
$decimals | int | Number of displayed decimal places (default: 2). |
$prefix | string\|null | Text prepended to the formatted amount (default: null). |
$suffix | string\|null | Text appended to the formatted amount (default: null). |
Return:
string - Formatted amount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric.
formatCents
Convert minor units to major units before applying the display format.
public static function formatCents(
string|int $cents,
int $decimals = 2,
int $divisor = 100,
?string $prefix = null,
?string $suffix = null
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$cents | string\|int | Amount in minor units, expressed as an integer or numeric string. |
$decimals | int | Number of displayed decimal places (default: 2). |
$divisor | int | Number of minor units per major unit (default: 100). |
$prefix | string\|null | Text prepended to the formatted amount (default: null). |
$suffix | string\|null | Text appended to the formatted amount (default: null). |
Return:
string - Formatted amount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric or the divisor is below one.
currency
Format an amount using Intl currency rules. If no locale is supplied, use the application locale with an en-US fallback.
public static function currency(
string|int|float $amount,
string $code = 'USD',
?string $locale = null
): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|int\|float | Numeric amount in major units. |
$code | string | Currency code such as USD (default: 'USD'). |
$locale | string\|null | Locale override (default: null). |
Return:
string|null - Localized amount, or null if formatting fails.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric.
Examples:
echo Money::currency('19.95', 'USD', 'en_US');discount
Subtract a percentage from the original amount.
public static function discount(
string|float|int $amount,
string|float|int $percentage,
?int $precision = null
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount in major units. |
$percentage | string\|float\|int | Percentage rate. |
$precision | int\|null | Number of decimal places for rounding. Pass null to skip rounding (default: null). |
Return:
float - Adjusted amount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount or percentage is not numeric, the percentage is negative, or the supplied precision is negative.
increase
Add a percentage to the original amount.
public static function increase(
string|float|int $amount,
string|float|int $percentage,
?int $precision = null
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount in major units. |
$percentage | string\|float\|int | Percentage rate. |
$precision | int\|null | Number of decimal places for rounding. Pass null to skip rounding (default: null). |
Return:
float - Adjusted amount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount or percentage is not numeric, the percentage is negative, or the supplied precision is negative.
tax
Add tax by applying the same percentage increase as increase().
public static function tax(
string|float|int $amount,
string|float|int $percentage,
?int $precision = null
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount in major units. |
$percentage | string\|float\|int | Percentage rate. |
$precision | int\|null | Number of decimal places for rounding. Pass null to skip rounding (default: null). |
Return:
float - Adjusted amount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount or percentage is not numeric, the percentage is negative, or the supplied precision is negative.
discounted
Recover the base from a total that already includes a percentage increase. Divide the total by 1 + percentage / 100, then calculate the difference. Despite the method name, this reverses an increase.
public static function discounted(
string|float|int $total,
string|float|int $percentage,
?int $precision = null
): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$total | string\|float\|int | Total inclusive of the percentage increase. |
$percentage | string\|float\|int | Percentage previously added. A rate of -100 makes the divisor zero. |
$precision | int\|null | Number of decimal places for rounding. Pass null to use two decimal places (default: null). |
Return:
array{amount: float, discount: float} - Rounded base amount and the rounded difference from the total.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the inputs are not numeric or the supplied precision is negative.
DivisionByZeroErrorif the percentage is-100.
Examples:
print_r(Money::discounted(108, 8)); // ['amount' => 100.0, 'discount' => 8.0]percentage
Calculate the value of a percentage of an amount.
public static function percentage(
string|float|int $percentage,
string|float|int $amount,
?int $precision = null
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$percentage | string\|float\|int | Percentage rate. |
$amount | string\|float\|int | Numeric amount in major units. |
$precision | int\|null | Number of decimal places for rounding. Pass null to skip rounding (default: null). |
Return:
float - Percentage amount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount or percentage is not numeric, the percentage is negative, or the supplied precision is negative.
round
Round an amount with PHP’s rounding function.
public static function round(
string|float|int $amount,
int $precision = 2,
int $mode = PHP_ROUND_HALF_UP
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount in major units. |
$precision | int | Number of decimal places for rounding (default: 2). |
$mode | int | PHP rounding mode (default: PHP_ROUND_HALF_UP). |
Return:
float - Rounded amount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric or the precision is negative.
sum
Add several amounts and optionally round the total.
public static function sum(
array $amounts,
?int $precision = null
): floatParameters:
| Parameter | Type | Description |
|---|---|---|
$amounts | array<int,string\|float\|int> | Amounts to add. |
$precision | int\|null | Number of decimal places for rounding. Pass null to skip rounding (default: null). |
Return:
float - Total amount.
Throws:
- Luminova\Exceptions\InvalidArgumentException if any amount is not numeric or the supplied precision is negative.
compare
Round both amounts to the requested precision before comparing them.
public static function compare(
string|float|int $first,
string|float|int $second,
int $precision = 2
): intParameters:
| Parameter | Type | Description |
|---|---|---|
$first | string\|float\|int | First numeric amount. |
$second | string\|float\|int | Second numeric amount. |
$precision | int | Number of decimal places for rounding (default: 2). |
Return:
int - -1 if the first amount is lower, 0 if equal, or 1 if higher.
Throws:
- Luminova\Exceptions\InvalidArgumentException if either amount is not numeric.
isZero
Compare an amount with zero after rounding.
public static function isZero(
string|float|int $amount,
int $precision = 2
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount in major units. |
$precision | int | Number of decimal places for rounding (default: 2). |
Return:
bool - true if the rounded amount compares equal to zero.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric.
split
Divide an amount into equal shares at the given precision. Apply the rounding difference to the first share.
public static function split(
string|float|int $amount,
int $parts,
int $precision = 2
): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount in major units. |
$parts | int | Number of shares, at least one. |
$precision | int | Number of decimal places for rounding (default: 2). |
Return:
float[] - Shares in allocation order.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric or the number of parts is below one.
allocate
Divide an amount in proportion to the supplied weights. Apply the rounding difference to the first share.
public static function allocate(
string|float|int $amount,
array $ratios,
int $precision = 2
): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$amount | string\|float\|int | Numeric amount in major units. |
$ratios | array<int,int\|float> | Nonempty list of weights whose sum is positive. |
$precision | int | Number of decimal places for rounding (default: 2). |
Return:
float[] - Shares in input order.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the amount is not numeric, the ratio list is empty, or its sum is nonpositive.
Examples:
print_r(Money::allocate(100, [3, 1])); // [75.0, 25.0]