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

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

prestashop/decimal
==================

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

1.5.0(4y ago)178.2M—5.8%8[1 issues](https://github.com/PrestaShop/decimal/issues)[1 PRs](https://github.com/PrestaShop/decimal/pulls)3MITPHPPHP &gt;=7.2

Since Aug 10Pushed 3y ago10 watchersCompare

[ Source](https://github.com/PrestaShop/decimal)[ Packagist](https://packagist.org/packages/prestashop/decimal)[ Docs](https://github.com/prestashop/decimal)[ RSS](/packages/prestashop-decimal/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (8)Used By (3)

Decimal
=======

[](#decimal)

[![Build status](https://github.com/PrestaShop/decimal/actions/workflows/php.yml/badge.svg)](https://github.com/PrestaShop/decimal/actions/workflows/php.yml)[![Coverage Status](https://camo.githubusercontent.com/b1453f13d99112c5e54bfc3dcd1d94ef3c65218064fdda48db4b9a47ad57bbe9/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f50726573746153686f702f646563696d616c2f62616467652e737667)](https://coveralls.io/github/PrestaShop/decimal)[![Total Downloads](https://camo.githubusercontent.com/68af28cabc12668e96ecac92fe743afa68e417f39a1a6690fcf1f32044c62e4b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70726573746173686f702f646563696d616c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/prestashop/decimal)

An object-oriented [BC Math extension](http://php.net/manual/en/book.bc.php) wrapper/shim.

**Decimal** offers a stateless, fluent object-oriented implementation of basic arbitrary-precision arithmetic, using BC Math if available.

You can find out more about floating point precision [here](http://php.net/float).

Example:

```
use PrestaShop\Decimal\DecimalNumber;
use PrestaShop\Decimal\Operation\Rounding;

echo (new DecimalNumber('0.1'))
    ->plus(new DecimalNumber('0.7'))
    ->times(new DecimalNumber('10'))
    ->round(0, Rounding::ROUND_FLOOR)

// echoes '8'
```

Install
-------

[](#install)

Via Composer

```
$ composer require prestashop/decimal
```

Usage reference
---------------

[](#usage-reference)

Quick links:

- [Instantiation](#instantiation)
- [Addition](#addition)
- [Subtraction](#subtraction)
- [Multiplication](#multiplication)
- [Division](#division)
- [Comparison](#comparison)
- [Fixed precision](#fixed-precision)
- [Rounding](#rounding)
- [Dot shifting](#dot-shifting)
- [Useful methods](#useful-methods)

### Instantiation

[](#instantiation)

Creates a new Decimal number.

```
public __construct ( string $number [, int $exponent = null ] ): DecimalNumber
```

There are two ways to instantiate a Decimal\\DecimalNumber:

```
// create a number from string
$number = new PrestaShop\Decimal\DecimalNumber('123.456');
echo $number; // echoes '123.456'
```

```
// exponent notation
$number = new PrestaShop\Decimal\DecimalNumber('123456', -3);
echo $number; // echoes '123.456'
```

### Addition

[](#addition)

Returns the computed result of adding another number to the current one.

```
public DecimalNumber::plus ( DecimalNumber $addend ): DecimalNumber
```

Examples:

```
$a = new PrestaShop\Decimal\DecimalNumber('123.456');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

echo $a->plus($b); // echoes '777.777'
```

### Subtraction

[](#subtraction)

Returns the computed result of subtracting another number to the current one.

```
public DecimalNumber::minus ( DecimalNumber $subtrahend ): DecimalNumber
```

Examples:

```
$a = new PrestaShop\Decimal\DecimalNumber('777.777');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

echo $a->minus($b); // echoes '123.456'
```

### Multiplication

[](#multiplication)

Returns the computed result of multiplying the current number with another one.

```
public DecimalNumber::times ( DecimalNumber $factor ): DecimalNumber
```

Examples:

```
$a = new PrestaShop\Decimal\DecimalNumber('777.777');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

echo $a->times($b); // echoes '508915.824417'
```

### Division

[](#division)

Returns the computed result of dividing the current number by another one, with up to a certain number of decimal positions (6 by default).

```
public DecimalNumber::dividedBy ( DecimalNumber $divisor [, int $precision = Operation\Division::DEFAULT_PRECISION ] )
```

Examples:

```
$a = new PrestaShop\Decimal\DecimalNumber('777.777');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

echo $a->dividedBy($b, 0);  // echoes '1'
echo $a->dividedBy($b, 5);  // echoes '1.18867'
echo $a->dividedBy($b, 10); // echoes '1.1886780341'
echo $a->dividedBy($b, 15); // echoes '1.188678034175886'
```

### Comparison

[](#comparison)

Returns the result of the comparison assertion.

```
$a = new PrestaShop\Decimal\DecimalNumber('777.777');
$b = new PrestaShop\Decimal\DecimalNumber('654.321');

$a->equals($b);                // returns false
$a->isLowerThan($b);           // returns false
$a->isLowerOrEqualThan($b);    // returns false
$a->isGreaterThan($b);         // returns true
$a->isGreaterOrEqualThan($b);  // returns true

// shortcut methods
$a->equalsZero();               // returns false
$a->isLowerThanZero();          // returns false
$a->isLowerOrEqualThanZero();   // returns false
$a->isGreaterThanZero();        // returns true
$a->isGreaterOrEqualThanZero(); // returns true
```

### Fixed precision

[](#fixed-precision)

Returns the number as a string, optionally rounded, with an exact number of decimal positions.

```
public DecimalNumber::toPrecision ( int $precision [, string $roundingMode = Rounding::ROUND_TRUNCATE ] ): string
```

Examples:

```
$a = new PrestaShop\Decimal\DecimalNumber('123.456');
$a = new PrestaShop\Decimal\DecimalNumber('-123.456');

// truncate / pad
$a->toPrecision(0); // '123'
$a->toPrecision(1); // '123.4'
$a->toPrecision(2); // '123.45'
$a->toPrecision(3); // '123.456'
$a->toPrecision(4); // '123.4560'
$b->toPrecision(0); // '-123'
$b->toPrecision(1); // '-123.4'
$b->toPrecision(2); // '-123.45'
$b->toPrecision(3); // '-123.456'
$b->toPrecision(4); // '-123.4560'

// ceil (round up)
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '124'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '123.5'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '123.46'
$b->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '-122'
$b->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '-123.3'
$b->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_CEIL); // '-123.44'

// floor (round down)
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '123.45'
$b->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '-124'
$b->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '-123.5'
$b->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_FLOOR); // '-123.46'

// half-up (symmetric half-up)
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '123.5'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '123.46'
$b->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '-123'
$b->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '-123.5'
$b->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_UP); // '-123.46'

// half-down (symmetric half-down)
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '123.46'
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '-123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '-123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_DOWN); // '-123.46'

// half-even
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '123.46'
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '-123'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '-123.4'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '-123.46'

$a = new PrestaShop\Decimal\DecimalNumber('1.1525354556575859505');
$a->toPrecision(0, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1'
$a->toPrecision(1, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.2'
$a->toPrecision(2, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.15'
$a->toPrecision(3, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.152'
$a->toPrecision(4, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.1525'
$a->toPrecision(5, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.15255'
$a->toPrecision(6, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.152535'
$a->toPrecision(7, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.1525354'
$a->toPrecision(8, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.15253546'
$a->toPrecision(9, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN);  // '1.152535456'
$a->toPrecision(10, PrestaShop\Decimal\Operation\Rounding::ROUND_HALF_EVEN); // '1.1525354556'
```

### Rounding

[](#rounding)

Rounding behaves like `toPrecision`, but provides "up to" a certain number of decimal positions (it does not add trailing zeroes).

```
public DecimalNumber::round ( int $maxDecimals [, string $roundingMode = Rounding::ROUND_TRUNCATE ] ): string
```

Examples:

```
$a = new PrestaShop\Decimal\DecimalNumber('123.456');
$a = new PrestaShop\Decimal\DecimalNumber('-123.456');

// truncate / pad
$a->round(0); // '123'
$a->round(1); // '123.4'
$a->round(2); // '123.45'
$a->round(3); // '123.456'
$a->round(4); // '123.456'
$b->round(0); // '-123'
$b->round(1); // '-123.4'
$b->round(2); // '-123.45'
$b->round(3); // '-123.456'
$b->round(4); // '-123.456'
```

### Dot shifting

[](#dot-shifting)

Creates a new copy of this number multiplied by 10^exponent

```
public DecimalNumber::toMagnitude ( int $exponent ): DecimalNumber
```

Examples:

```
$a = new PrestaShop\Decimal\DecimalNumber('123.456789');

// shift 3 digits to the left
$a->toMagnitude(-3); // 0.123456789

// shift 3 digits to the right
$a->toMagnitude(3); // 123456.789
```

### Useful methods

[](#useful-methods)

```
$number = new PrestaShop\Decimal\DecimalNumber('123.45');
$number->getIntegerPart();    // '123'
$number->getFractionalPart(); // '45'
$number->getPrecision();      // '2' (number of decimals)
$number->getSign();           // '' ('-' if the number was negative)
$number->getExponent();       // '2' (always positive)
$number->getCoefficient();    // '123456'
$number->isPositive();        // true
$number->isNegative();        // false
$number->invert();            // new Decimal\DecimalNumber('-123.45')
```

Testing
-------

[](#testing)

```
$ composer install
$ vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [All Contributors](https://github.com/prestashop/decimal/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity55

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 52.1% 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 ~327 days

Recently: every ~406 days

Total

6

Last Release

1567d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.4

1.5.0PHP &gt;=7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15106407?v=4)[Jarvis](/maintainers/ps-jarvis)[@ps-jarvis](https://github.com/ps-jarvis)

![](https://avatars.githubusercontent.com/u/1009343?v=4)[Pablo Borowicz](/maintainers/eternoendless)[@eternoendless](https://github.com/eternoendless)

---

Top Contributors

[![eternoendless](https://avatars.githubusercontent.com/u/1009343?v=4)](https://github.com/eternoendless "eternoendless (38 commits)")[![PierreRambaud](https://avatars.githubusercontent.com/u/1462701?v=4)](https://github.com/PierreRambaud "PierreRambaud (17 commits)")[![Progi1984](https://avatars.githubusercontent.com/u/1533248?v=4)](https://github.com/Progi1984 "Progi1984 (11 commits)")[![atomiix](https://avatars.githubusercontent.com/u/2168836?v=4)](https://github.com/atomiix "atomiix (4 commits)")[![matks](https://avatars.githubusercontent.com/u/3830050?v=4)](https://github.com/matks "matks (2 commits)")[![FabienPapet](https://avatars.githubusercontent.com/u/1446265?v=4)](https://github.com/FabienPapet "FabienPapet (1 commits)")

---

Tags

arbitrary-precisionbcmathcore-dependencyhacktoberfestphpphp-librarydecimalmathprecisionbcmathprestashop

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[phpseclib/bcmath_compat

PHP 5.x-8.x polyfill for bcmath extension

16720.7M17](/packages/phpseclib-bcmath-compat)[php-decimal/php-decimal

Correctly-rounded arbitrary precision decimal floating point

781.0M9](/packages/php-decimal-php-decimal)[aza/math

AzaMath - Anizoptera CMF mathematic component. Arbitrary precision arithmetic (for huge integers; BCMath wrapper) and universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, can use GMP and core PHP functions for speed optimization).

1921.9k1](/packages/aza-math)[rtlopez/decimal

An object oriented immutable arbitrary-precision arithmetic library for PHP

27262.8k2](/packages/rtlopez-decimal)

PHPackages © 2026

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