PHPackages                             jedstrom/php-decimal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jedstrom/php-decimal

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jedstrom/php-decimal
====================

PHP Decimal Type implementation

1.0.1(10y ago)127BSD-3-ClausePHPPHP &gt;=5.5.0

Since Nov 26Pushed 9y ago1 watchersCompare

[ Source](https://github.com/jedstrom/php-decimal)[ Packagist](https://packagist.org/packages/jedstrom/php-decimal)[ RSS](/packages/jedstrom-php-decimal/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

jedstrom/php-decimal
====================

[](#jedstromphp-decimal)

A PHP Decimal Type implementation
---------------------------------

[](#a-php-decimal-type-implementation)

A value object class that utilizes BCMath functions to perform arbitrary precision arithmetic operations.

Usage
-----

[](#usage)

### Supported Operations

[](#supported-operations)

The following operations are currently supported:

1. Addition
2. Subtraction
3. Multiplication
4. Division

**Source**

```
$termOne = new Decimal('2', 2);
$termTwo = new Decimal('3', 0);
var_dump($termOne->add($termTwo)->getValue());
var_dump($termOne->subtract($termTwo)->getValue());
var_dump($termOne->multiply($termTwo)->getValue());
var_dump($termOne->divide($termTwo)->getValue());
// refer to Mixed Precision Calculations section
var_dump($termTwo->add($termOne)->getValue());
var_dump($termTwo->subtract($termOne)->getValue());
var_dump($termTwo->multiply($termOne)->getValue());
var_dump($termTwo->divide($termOne)->getValue());

```

**Output**

```
string(4) "5.00"
string(4) "-1.00"
string(4) "6.00"
string(4) "0.66"
string(4) "5"
string(4) "1"
string(4) "6"
string(4) "2"

```

### Automatic Precision Determination

[](#automatic-precision-determination)

If the precision is not explicitly set, the precision required to represent the value will be automatically calculated.

**Source**

```
$decimal = new Decimal('3.14159');
var_dump($decimal->getPrecision());

```

**Output**

```
int(5)

```

### Rounding Methodology

[](#rounding-methodology)

Rounding is performed using the half away from zero method.

**Source**

```
$termOne = new Decimal('1');
$termTwo = new Decimal('1.5');
$product = $termOne->multiply($termTwo);
var_dump($product->getValue());

```

**Output**

```
string(1) "2"

```

**Source**

```
$termOne = new Decimal('-1');
$termTwo = new Decimal('1.5');
$product = $termOne->multiply($termTwo);
var_dump($product->getValue());

```

**Output**

```
string(1) "-2"

```

### Precision Truncation

[](#precision-truncation)

If a precision is explicitly specified and the value specified requires more precision than specified, the value will be rounded to the specified precision.

**Source**

```
$decimal = new Decimal('3.14159', 3);
var_dump($decimal->getValue());

```

**Output**

```
string(5) "3.142"

```

### Mixed Precision Calculations

[](#mixed-precision-calculations)

Calculations are performed internally at the maximum precision of either term + 1 and then rounded to the precision of the first term.

So, in the following example:

```
19.99 x 0.07125

```

The internal result is:

```
1.424287

```

And the output is rounded to the precision of the first term (19.99 =&gt; 2) to produce:

```
1.42

```

**Source**

```
$price   = new Decimal('19.99', 2);
$taxRate = new Decimal('0.07125', 5');
$tax     = $price->multiply($taxRate);
var_dump($tax);

```

**Output**

```
string(4) "1.42"

```

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3862d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2516606?v=4)[jedstrom](/maintainers/jedstrom)[@jedstrom](https://github.com/jedstrom)

---

Top Contributors

[![jedstrom](https://avatars.githubusercontent.com/u/2516606?v=4)](https://github.com/jedstrom "jedstrom (49 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jedstrom-php-decimal/health.svg)

```
[![Health](https://phpackages.com/badges/jedstrom-php-decimal/health.svg)](https://phpackages.com/packages/jedstrom-php-decimal)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
