Decimal
in package
Helpers for exact decimal arithmetic. Floats are rejected on purpose, see rejectFloat().
Table of Contents
Constants
- PLAIN_PATTERN : string = '/^[+-]?(?:\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
- format() : string
- Plain string without exponent, trailing zeros removed down to $minScale decimals.
- isPlain() : bool
- Whether a string is a decimal number in plain notation, see PLAIN_PATTERN.
- isZero() : bool
- of() : BigDecimal
- ofNullable() : BigDecimal|null
- rejectFloat() : T
- 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() : BigDecimal
- Commercial rounding (half up, away from zero), as required by EN 16931.
Constants
PLAIN_PATTERN
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.
public
string
PLAIN_PATTERN
= '/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/'
Methods
format()
Plain string without exponent, trailing zeros removed down to $minScale decimals.
public
static format(BigDecimal $value[, int<0, max> $minScale = 0 ]) : string
Parameters
- $value : BigDecimal
- $minScale : int<0, max> = 0
Return values
stringisPlain()
Whether a string is a decimal number in plain notation, see PLAIN_PATTERN.
public
static isPlain(string $value) : bool
Parameters
- $value : string
Return values
boolisZero()
public
static isZero(BigDecimal $value) : bool
Parameters
- $value : BigDecimal
Return values
boolof()
public
static of(BigNumber|int|float|string $value) : BigDecimal
Parameters
- $value : BigNumber|int|float|string
-
floats are rejected, see rejectFloat(); strings must use plain decimal notation, see PLAIN_PATTERN
Tags
Return values
BigDecimalofNullable()
public
static ofNullable(BigNumber|int|float|string|null $value) : BigDecimal|null
Parameters
- $value : BigNumber|int|float|string|null
Return values
BigDecimal|nullrejectFloat()
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).
public
static rejectFloat(T|float $value) : T
Parameters
- $value : T|float
Tags
Return values
Tround()
Commercial rounding (half up, away from zero), as required by EN 16931.
public
static round(BigDecimal $value, int<0, max> $scale) : BigDecimal
Parameters
- $value : BigDecimal
- $scale : int<0, max>