PHPackages                             egorsmkv-packagist/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. egorsmkv-packagist/decimal

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

egorsmkv-packagist/decimal
==========================

An object oriented immutable arbitrary-precision arithmetic library for PHP

v1.0(8y ago)129MITPHPPHP &gt;=5.3.0

Since Dec 22Pushed 8y agoCompare

[ Source](https://github.com/egorsmkv/decimal)[ Packagist](https://packagist.org/packages/egorsmkv-packagist/decimal)[ Docs](https://github.com/egorsmkv/decimal)[ RSS](/packages/egorsmkv-packagist-decimal/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Decimal
=======

[](#decimal)

An object oriented arbitrary precision arithmetic class for PHP (&gt;5.3.3).

Installation
------------

[](#installation)

You can use [Composer](http://getcomposer.org/)

To add this package as a local, per-project dependency to your project, simply add a dependency on `rtlopez/decimal` to your project's `composer.json` file. Here is a minimal example of a `composer.json` file that just defines a dependency on `Decimal`:

```
{
  "require": {
    "rtlopez/decimal": "dev-master"
  }
}
```

Features
--------

[](#features)

The `Decimal` class can be used for an arbitrary precision calculation. Main features are:

- Immutable result of each operation,
- The same result precision,
- Safe for finance calculations,
- Fluent interface,
- Many unit test (more than 1100 tests and 3700 assertions),
- Easy to use and install,

Why should I use it?
--------------------

[](#why-should-i-use-it)

The `Decimal` solve several inconveniences known in other solutions.

- `Float` is only an aproximation and have limited precision,
- `Fixed-point` have limited size (64-bits on 64-bit system),
- Pure `bcmath` cannot correctly round, only truncate,
- `GMP` is not object-oriented

Usage
-----

[](#usage)

### Creating objects with simple factory

[](#creating-objects-with-simple-factory)

```
use RtLopez\Decimal;

$n1 = Decimal::create(123, 2);
$n2 = Decimal::create(123.45, 2); // not recommended
$n3 = Decimal::create('123.45', 2);
$n4 = Decimal::create($n1, 2);
```

### Printing and formatting numbers

[](#printing-and-formatting-numbers)

```
$number = Decimal::create('12345.671', 2);

echo (string)$number;
// 12345.67

echo $number->format(1, ',', ' ');
// 12 345,7

echo $number->truncate()->format(null, ',', ' ', false);
// 12 345,00
```

### Arithmetics

[](#arithmetics)

```
$n1 = Decimal::create('12345.671', 3);
$n2 = Decimal::create('11111.111', 3);
$n3 = $n1->add($n2)->mul(-1);
echo $n3; // -23456.782

// fluid interface
$n4 = $n3->pow(2)->sqrt()->mul(-2)->div(2)->abs();
```

### Immutable

[](#immutable)

```
$n1 = Decimal::create('12345.671', 3);
$n1->add(2);
...
echo $n1;
// 12345.671
```

### Comparisions

[](#comparisions)

```
$n1 = Decimal::create('12345.671', 3);
$n2 = Decimal::create('11111.111', 3);
$n1->gt($n2);        // true
$n1->eq(0);          // false
$n2->le(100);        // false
```

### Rounding

[](#rounding)

```
$n = Decimal::create('12345.671', 3);
echo $n->round(2);   // 12345.67
echo $n->round(1);   // 12345.7
echo $n->truncate(); // 12345
echo $n->ceil(1);    // 12345.6
echo $n->floor();    // 12346
```

### Implementations

[](#implementations)

There are exists three internal implementations of this library.

- `RtLopez\DecimalBCMath`: (default)based on bcmath library
- `RtLopez\DecimalFixed`: based on scaled integers
- `RtLopez\DecimalFloat`: based on floats

Only `BCMath` implementation works correctly in wide spectrum of numbers. All others can fail in many border cases. They was implemented only as proof of concept and you can using it only for your own risk.

The default implementation or precision can be changed by functions:

```
Decimal::setDefaultImplementation('RtLopez\DecimalBCMath');
Decimal::setDefaultPrecision(6);
```

Licence
-------

[](#licence)

This library is distributed under MIT Licence.

Issue reporting or feature request
----------------------------------

[](#issue-reporting-or-feature-request)

Coming soon.

TODO
----

[](#todo)

- GMP implementation

Donation
--------

[](#donation)

BTC: 1Lopez7yPtbyjfLGe892JfheDFJMMt43tW LTC: LV3G3sJxz9AYpDMYUp8e1LCmerFYxVY3ak

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 86.4% 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

Unknown

Total

1

Last Release

3112d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dfc375866ae937a820a056fce1a93cb810621859528635d643214a60fe7006bd?d=identicon)[egorsmkv-packagist](/maintainers/egorsmkv-packagist)

---

Top Contributors

[![rtlopez](https://avatars.githubusercontent.com/u/1812055?v=4)](https://github.com/rtlopez "rtlopez (19 commits)")[![egorsmkv](https://avatars.githubusercontent.com/u/7875085?v=4)](https://github.com/egorsmkv "egorsmkv (2 commits)")[![l3n641](https://avatars.githubusercontent.com/u/14206875?v=4)](https://github.com/l3n641 "l3n641 (1 commits)")

---

Tags

moneydecimalbignumimmutablenumbersnumericbcmathgmpfinancialbig decimallarge numbers

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/egorsmkv-packagist-decimal/health.svg)

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

###  Alternatives

[rtlopez/decimal

An object oriented immutable arbitrary-precision arithmetic library for PHP

27270.4k2](/packages/rtlopez-decimal)[brick/math

Arbitrary-precision arithmetic library

2.2k532.9M378](/packages/brick-math)[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

23.8k69.4k](/packages/grumpydictator-firefly-iii)[kwn/number-to-words

Multi language standalone PHP number to words converter. Fully tested, open for extensions and new languages.

4285.3M23](/packages/kwn-number-to-words)[firefly-iii/data-importer

Firefly III Data Import Tool.

8005.8k](/packages/firefly-iii-data-importer)[prestashop/decimal

Object-oriented wrapper/shim for BC Math PHP extension. Allows for arbitrary-precision math operations.

178.7M7](/packages/prestashop-decimal)

PHPackages © 2026

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