Luminova Framework

PHP Luminova: Convert, Format, and Split Money

Last updated: 2026-11-28 20:57:12

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
): int

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount in major units.
$divisorintNumber of minor units per major unit (default: 100).

Return:

int - Amount in minor units.

Throws:


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
): string

Parameters:

ParameterTypeDescription
$centsstring\|intAmount in minor units, expressed as an integer or numeric string.
$divisorintNumber of minor units per major unit (default: 100).

Return:

string - Amount in major units without thousands separators.

Throws:


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
): string

Parameters:

ParameterTypeDescription
$amountstring\|int\|floatNumeric amount in major units.
$decimalsintNumber of displayed decimal places (default: 2).
$prefixstring\|nullText prepended to the formatted amount (default: null).
$suffixstring\|nullText appended to the formatted amount (default: null).

Return:

string - Formatted amount.

Throws:


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
): string

Parameters:

ParameterTypeDescription
$centsstring\|intAmount in minor units, expressed as an integer or numeric string.
$decimalsintNumber of displayed decimal places (default: 2).
$divisorintNumber of minor units per major unit (default: 100).
$prefixstring\|nullText prepended to the formatted amount (default: null).
$suffixstring\|nullText appended to the formatted amount (default: null).

Return:

string - Formatted amount.

Throws:


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
): ?string

Parameters:

ParameterTypeDescription
$amountstring\|int\|floatNumeric amount in major units.
$codestringCurrency code such as USD (default: 'USD').
$localestring\|nullLocale override (default: null).

Return:

string|null - Localized amount, or null if formatting fails.

Throws:

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
): float

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount in major units.
$percentagestring\|float\|intPercentage rate.
$precisionint\|nullNumber of decimal places for rounding. Pass null to skip rounding (default: null).

Return:

float - Adjusted amount.

Throws:


increase

Add a percentage to the original amount.

public static function increase(
    string|float|int $amount,
    string|float|int $percentage,
    ?int $precision = null
): float

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount in major units.
$percentagestring\|float\|intPercentage rate.
$precisionint\|nullNumber of decimal places for rounding. Pass null to skip rounding (default: null).

Return:

float - Adjusted amount.

Throws:


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
): float

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount in major units.
$percentagestring\|float\|intPercentage rate.
$precisionint\|nullNumber of decimal places for rounding. Pass null to skip rounding (default: null).

Return:

float - Adjusted amount.

Throws:


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
): array

Parameters:

ParameterTypeDescription
$totalstring\|float\|intTotal inclusive of the percentage increase.
$percentagestring\|float\|intPercentage previously added. A rate of -100 makes the divisor zero.
$precisionint\|nullNumber 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:

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
): float

Parameters:

ParameterTypeDescription
$percentagestring\|float\|intPercentage rate.
$amountstring\|float\|intNumeric amount in major units.
$precisionint\|nullNumber of decimal places for rounding. Pass null to skip rounding (default: null).

Return:

float - Percentage amount.

Throws:


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
): float

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount in major units.
$precisionintNumber of decimal places for rounding (default: 2).
$modeintPHP rounding mode (default: PHP_ROUND_HALF_UP).

Return:

float - Rounded amount.

Throws:


sum

Add several amounts and optionally round the total.

public static function sum(
    array $amounts,
    ?int $precision = null
): float

Parameters:

ParameterTypeDescription
$amountsarray<int,string\|float\|int>Amounts to add.
$precisionint\|nullNumber of decimal places for rounding. Pass null to skip rounding (default: null).

Return:

float - Total amount.

Throws:


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
): int

Parameters:

ParameterTypeDescription
$firststring\|float\|intFirst numeric amount.
$secondstring\|float\|intSecond numeric amount.
$precisionintNumber of decimal places for rounding (default: 2).

Return:

int - -1 if the first amount is lower, 0 if equal, or 1 if higher.

Throws:


isZero

Compare an amount with zero after rounding.

public static function isZero(
    string|float|int $amount,
    int $precision = 2
): bool

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount in major units.
$precisionintNumber of decimal places for rounding (default: 2).

Return:

bool - true if the rounded amount compares equal to zero.

Throws:


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
): array

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount in major units.
$partsintNumber of shares, at least one.
$precisionintNumber of decimal places for rounding (default: 2).

Return:

float[] - Shares in allocation order.

Throws:


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
): array

Parameters:

ParameterTypeDescription
$amountstring\|float\|intNumeric amount in major units.
$ratiosarray<int,int\|float>Nonempty list of weights whose sum is positive.
$precisionintNumber of decimal places for rounding (default: 2).

Return:

float[] - Shares in input order.

Throws:

Examples:

print_r(Money::allocate(100, [3, 1])); // [75.0, 25.0]