Download the PHP package andkom/php-decimal without Composer
On this page you can find all versions of the php package andkom/php-decimal. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download andkom/php-decimal
More information about andkom/php-decimal
Files in andkom/php-decimal
Package php-decimal
Short Description Arbitrary-precision decimal arithmetic library for PHP, built on top of bcmath.
License MIT
Informations about the package php-decimal
php-decimal
Arbitrary-precision decimal arithmetic library for PHP, built on top of the bcmath extension.
Provides both mutable (Decimal) and immutable (DecimalImmutable) implementations with a full set of arithmetic operations, rounding modes, comparisons, and formatting options.
Requirements
- PHP 7.4+
bcmathextension
Installation
Quick Start
Creating Decimals
| Method | Description |
|---|---|
new Decimal($value, $scale) |
Constructor, value is a string, scale is optional |
Decimal::create($value, $scale) |
Factory, auto-detects type (string, int, float, Decimal) |
Decimal::createFromString($value, $scale) |
From string, supports scientific notation (e.g. '1.5e-3') |
Decimal::createFromFloat($value, $scale) |
From float, auto-detects scale if not provided |
Decimal::createFromInteger($value, $scale) |
From integer |
Decimal::createFromDecimal($decimal, $scale) |
Clone from another Decimal |
Default Scale
Arithmetic
All arithmetic methods accept an optional $scale parameter to override the result scale.
| Method | Description |
|---|---|
add($value, $scale) |
Addition |
subtract($value, $scale) |
Subtraction |
multiply($value, $scale) |
Multiplication |
divide($value, $scale) |
Division |
modulus($value) |
Modulo (remainder) |
power($value, $scale) |
Exponentiation (supports negative exponents) |
squareRoot($scale) |
Square root |
Rounding
All rounding methods accept an optional $precision parameter. Positive precision rounds decimal places, negative precision rounds to powers of 10.
| Method | Description |
|---|---|
round($precision, $mode) |
Round with specified mode (default: ROUND_HALF_UP) |
roundUp($precision) |
Round away from zero |
roundDown($precision) |
Round toward zero (truncation) |
roundHalfUp($precision) |
Round half away from zero |
roundHalfDown($precision) |
Round half toward zero |
roundHalfEven($precision) |
Round half to even (banker's rounding) |
roundHalfOdd($precision) |
Round half to odd |
floor($precision) |
Round toward negative infinity |
ceil($precision) |
Round toward positive infinity |
truncate($precision) |
Truncate toward zero |
Rounding Mode Constants
| Constant | Value |
|---|---|
ROUND_UP |
1 |
ROUND_DOWN |
2 |
ROUND_HALF_UP |
3 |
ROUND_HALF_DOWN |
4 |
ROUND_HALF_EVEN |
5 |
ROUND_HALF_ODD |
6 |
Sign Operations
| Method | Description |
|---|---|
inverse() |
Flip sign (positive becomes negative and vice versa) |
negate() |
Always return negative value |
absolutize() |
Always return positive value (absolute value) |
Conversion & Formatting
| Method | Description |
|---|---|
toFloat() |
Convert to PHP float |
toInt() |
Convert to PHP int (truncates decimal part) |
toString($trailingZeros) |
String without trailing zeros (default), or with |
toFixed($precision) |
String with exact decimal places (does not mutate) |
toFormat($precision, $decPoint, $thousandsSep, $trailingZeros, $mode) |
Formatted number with thousands separator |
toScientific($precision, $exponent) |
Scientific notation (e.g. '1.23E4') |
toDigits($digits) |
Round to N significant digits |
toDecimal() |
Clone as new instance of same type |
toMutable() |
Convert to mutable Decimal |
toImmutable() |
Convert to immutable DecimalImmutable |
Comparisons
All comparison methods accept an optional $scale parameter.
| Method | Description |
|---|---|
compareTo($arg, $scale) |
Returns -1, 0, or 1 |
isEqual($arg, $scale) |
Equal to |
isNotEqual($arg, $scale) |
Not equal to |
isGreaterThan($arg, $scale) |
Greater than |
isGreaterThanOrEquals($arg, $scale) |
Greater than or equal |
isLessThan($arg, $scale) |
Less than |
isLessThanOrEquals($arg, $scale) |
Less than or equal |
Predicates
| Method | Description |
|---|---|
isInteger() |
True if no fractional part |
isDecimal() |
True if has fractional part |
isPositive() |
True if greater than zero |
isNegative() |
True if less than zero |
isZero() |
True if equal to zero |
isNotZero() |
True if not equal to zero |
isEven() |
True if integer part is even |
isOdd() |
True if integer part is odd |
Static Helpers
| Method | Description |
|---|---|
Decimal::max(...$args) |
Returns the maximum value |
Decimal::min(...$args) |
Returns the minimum value |
Mutable vs Immutable
Decimal is mutable -- operations modify the instance in place and return $this:
DecimalImmutable always returns a new instance, leaving the original unchanged:
Convert between them with toMutable() and toImmutable().
Running Tests
All versions of php-decimal with dependencies
ext-bcmath Version *