Skip to content

API reference / Quittio \ Core \ Support

Decimal

final class in quittio/core

Quittio\Core\Support\Decimal · Source

Helpers for exact decimal arithmetic. Floats are rejected on purpose, see rejectFloat().

Constants#

ConstantValueDescription
PLAIN_PATTERN'/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/'Plain decimal notation (xs:decimal): optional sign, digits and decimal point. No exponent, so that "1e999999999" cannot expand to a billion digits, and no thousands separators.

Methods#

MethodDescription
::format()Plain string without exponent, trailing zeros removed down to $minScale decimals.
::isPlain()Whether a string is a decimal number in plain notation, see PLAIN_PATTERN.
::isZero()
::of()
::ofNullable()
::rejectFloat()Passes numbers through that are converted later and rejects floats right away. Public methods accept float in their signature only to reject it here: without it, PHP would silently cut 19.99 to 19 in files without strict types (int|string coercion).
::round()Commercial rounding (half up, away from zero), as required by EN 16931.

format()#

public static function format(BigDecimal $value, int<0, max> $minScale = 0): string

static

Plain string without exponent, trailing zeros removed down to $minScale decimals.

ParameterTypeDescription
$valueBrick\Math\BigDecimal
$minScaleint<0, max> = 0
Returns
string

isPlain()#

public static function isPlain(string $value): bool

static

Whether a string is a decimal number in plain notation, see PLAIN_PATTERN.

ParameterTypeDescription
$valuestring
Returns
bool

isZero()#

public static function isZero(BigDecimal $value): bool

static

ParameterTypeDescription
$valueBrick\Math\BigDecimal
Returns
bool

of()#

public static function of(BigNumber|int|float|string $value): BigDecimal

static

ParameterTypeDescription
$valueBrick\Math\BigNumber|int|float|stringfloats are rejected, see rejectFloat(); strings must use plain decimal notation, see PLAIN_PATTERN
Returns
Brick\Math\BigDecimal
Throws
Brick\Math\Exception\NumberFormatException – for strings in another notation

ofNullable()#

public static function ofNullable(BigNumber|int|float|string|null $value): ?BigDecimal

static

ParameterTypeDescription
$valueBrick\Math\BigNumber|int|float|string|null
Returns
?Brick\Math\BigDecimal

rejectFloat()#

public static function rejectFloat(T|float $value): T

static

Passes numbers through that are converted later and rejects floats right away. Public methods accept float in their signature only to reject it here: without it, PHP would silently cut 19.99 to 19 in files without strict types (int|string coercion).

ParameterTypeDescription
$valueQuittio\Core\Support\T|float
Returns
Quittio\Core\Support\T

round()#

public static function round(BigDecimal $value, int<0, max> $scale): BigDecimal

static

Commercial rounding (half up, away from zero), as required by EN 16931.

ParameterTypeDescription
$valueBrick\Math\BigDecimal
$scaleint<0, max>
Returns
Brick\Math\BigDecimal

The open core packages are MIT licensed. The legal wording of the country packs must be confirmed by a tax adviser.