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#
| Constant | Value | Description |
|---|---|---|
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#
| Method | Description |
|---|---|
::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): stringPlain string without exponent, trailing zeros removed down to $minScale decimals.
| Parameter | Type | Description |
|---|---|---|
$value | Brick\Math\BigDecimal | |
$minScale | int<0, max> = 0 |
- Returns
string
isPlain()#
public static function isPlain(string $value): boolWhether a string is a decimal number in plain notation, see PLAIN_PATTERN.
| Parameter | Type | Description |
|---|---|---|
$value | string |
- Returns
bool
isZero()#
public static function isZero(BigDecimal $value): bool| Parameter | Type | Description |
|---|---|---|
$value | Brick\Math\BigDecimal |
- Returns
bool
of()#
public static function of(BigNumber|int|float|string $value): BigDecimal| Parameter | Type | Description |
|---|---|---|
$value | Brick\Math\BigNumber|int|float|string | floats 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| Parameter | Type | Description |
|---|---|---|
$value | Brick\Math\BigNumber|int|float|string|null |
- Returns
?Brick\Math\BigDecimal
rejectFloat()#
public static function rejectFloat(T|float $value): TPasses 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).
| Parameter | Type | Description |
|---|---|---|
$value | Quittio\Core\Support\T|float |
- Returns
Quittio\Core\Support\T
round()#
public static function round(BigDecimal $value, int<0, max> $scale): BigDecimalCommercial rounding (half up, away from zero), as required by EN 16931.
| Parameter | Type | Description |
|---|---|---|
$value | Brick\Math\BigDecimal | |
$scale | int<0, max> |
- Returns
Brick\Math\BigDecimal