Skip to content

API reference ​

Class PhpCollective\DecimalObject\Decimal, implementing JsonSerializable and Stringable.

$value arguments accept a Decimal, a numeric string, an integer or a float unless noted. Methods that return a Decimal return a new instance of the called class.

Creation ​

MethodDescription
new Decimal($value, ?int $scale = null, bool $strict = false)Creates a decimal. Also accepts any Stringable with a numeric string.
Decimal::create($value, ?int $scale = null, bool $strict = false): staticCreates a decimal; without a scale, clones an instance of the called class while preserving its declared scale.
Decimal::tryCreate($value, ?int $scale = null): ?staticReturns null for null or input rejected with InvalidArgumentException.
Decimal::createFromFloat(float $value): staticKeeps all digits of the float, independent of the precision ini setting.
Decimal::zero(): static, Decimal::one(): staticThe values 0 and 1.

See Creating decimals.

Arithmetic ​

MethodDescriptionDefault scale
add($value, ?int $scale = null): staticSumlarger of both
subtract($value, ?int $scale = null): staticDifferencelarger of both
multiply($value, ?int $scale = null): staticProductsum of both
divide($value, int $scale, ?int $roundMode = null): staticQuotient, cut off at $scale, or rounded when a mode is givenrequired
divideWithRemainder($value): arrayWhole number quotient and remainder0, and larger of both
mod($value, ?int $scale = null): staticRemainderscale of the value
pow($exponent, ?int $scale = null): staticPower. $exponent is a whole number.scale of the value
sqrt(?int $scale = null): staticSquare rootscale of the value
shift(int $places): staticMoves the decimal point, multiplying by a power of tenfollows the digits
absolute(): staticValue without signunchanged
negate(): staticValue with the opposite signunchanged
Static methodDescription
Decimal::sum(iterable $values): staticSum, 0 for an empty list
Decimal::average(iterable $values, int $scale): staticMean, cut off at $scale
Decimal::min(iterable $values): staticSmallest value
Decimal::max(iterable $values): staticLargest value
MethodDescription
allocate(array $ratios): arrayShares by ratio that sum to the value exactly, keys preserved
split(int $parts): arrayEqual shares that sum to the value exactly

See Arithmetic.

Rounding ​

MethodDescription
round(int $scale = 0, int $roundMode = Decimal::ROUND_HALF_UP): staticRounds to $scale decimal places, or to a whole number with ROUND_FLOOR and ROUND_CEIL. A negative scale rounds to tens, hundreds and so on.
withScale(int $scale, int $roundMode = Decimal::ROUND_HALF_UP): staticPads or rounds to exactly $scale decimal places, in any mode.
roundToStep($step, int $roundMode = Decimal::ROUND_HALF_UP): staticRounds to the nearest multiple of $step.
floor(): staticNearest integer towards negative infinity.
ceil(): staticNearest integer towards positive infinity.
truncate(int $scale = 0): staticCuts off digits beyond $scale.
trim(): staticRemoves trailing zeros after the decimal point.
ConstantValue
Decimal::ROUND_HALF_UP1, same as PHP_ROUND_HALF_UP
Decimal::ROUND_CEIL7
Decimal::ROUND_FLOOR8
Decimal::ROUND_HALF_DOWN9
Decimal::ROUND_HALF_EVEN10
Decimal::ROUND_HALF_ODD11
Decimal::ROUND_DOWN12, towards zero
Decimal::ROUND_UP13, away from zero
Decimal::MAX_EXPONENT10000, limit for scientific notation input

See Rounding.

Comparison ​

MethodDescription
compareTo($value): int-1, 0 or 1
equals($value): boolSame value, scale ignored
greaterThan($value): bool
greaterThanOrEquals($value): bool
lessThan($value): bool
lessThanOrEquals($value): bool
between($min, $max, bool $inclusive = true): boolWithin the range
clamp($min, $max): staticThe value, or the nearest bound when outside the range

See Comparison and checks.

Checks ​

MethodDescription
isZero(): boolValue is zero at any scale
isPositive(): boolGreater than zero
isNegative(): boolLess than zero
isInteger(): boolNo decimals other than zeros
isEven(): boolWhole number divisible by two
isOdd(): boolWhole number not divisible by two
isBigInteger(): boolIntegral part does not fit a PHP integer
isBigDecimal(): booltoFloat() would refuse the value
sign(): int-1, 0 or 1
scale(): intNumber of decimal places

Conversion ​

MethodDescription
toString(): stringExact decimal string, also used for (string) casts
toInt(): intInteger without decimals. Throws TypeError when out of range.
toFloat(): floatFloat approximation. Throws TypeError for big decimals.
toScientific(): stringScientific notation such as -2.2345e1
jsonSerialize(): stringSame as toString()

See Conversion.

Exceptions ​

SituationException
Input is not numeric, or a float is INF or NANInvalidArgumentException
Exponent beyond Decimal::MAX_EXPONENTInvalidArgumentException
Negative scale in the constructor, withScale(), truncate() or round() with ROUND_FLOOR / ROUND_CEILInvalidArgumentException
Unknown mode in withScale()InvalidArgumentException
Empty list in average(), min(), max(); invalid ratios in allocate(); clamp() with $min above $maxInvalidArgumentException
Strict mode and the input has more decimals than the scaleInvalidArgumentException
divide(), divideWithRemainder() or mod() by zeroDivisionByZeroError
Unknown mode in divide() or roundToStep(); negative scale in divide() with a mode; roundToStep() with a step of zero or belowInvalidArgumentException
toInt() or toFloat() on a value that does not fitTypeError
sqrt() of a negative value, pow() with a fractional exponent, negative scale in an arithmetic methodValueError from bcmath

Deprecated ​

MethodUse instead
greatherThanOrEquals()greaterThanOrEquals()

Released under the MIT License.