PHPackages                             mehr-it/php-decimals - 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. mehr-it/php-decimals

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

mehr-it/php-decimals
====================

Easy to use arithmetic operations for decimals in PHP (using BCMath)

2.6.1(3y ago)13.8k↓66.7%3MITPHPPHP &gt;=7.2CI failing

Since Nov 12Pushed 3y agoCompare

[ Source](https://github.com/mehr-it/php-decimals)[ Packagist](https://packagist.org/packages/mehr-it/php-decimals)[ RSS](/packages/mehr-it-php-decimals/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (20)Used By (3)

PHP Decimals
============

[](#php-decimals)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2ccfefe55324713ced72e38a2325a6cac473d9d934687512b8b19ae0ddb71b4c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6568722d69742f7068702d646563696d616c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mehr-it/php-decimals)[![Build Status](https://camo.githubusercontent.com/2581befb5e4db3a18669be096512814960cad7667533c8181a5abc6a561625dd/68747470733a2f2f7472617669732d63692e6f72672f6d6568722d69742f7068702d646563696d616c732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mehr-it/php-decimals)

This library is a small wrapper around PHP's **[BCMath](https://www.php.net/manual/en/book.bc.php)** extension. BCMath implements arbitrary precision mathematics, it's usage is not very comfortable.

This library aims to implement an easy to use interface for the most commonly needed mathematical operations using BCMath internally.

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

[](#installation)

Install the package using composer:

```
composer require mehr-it/php-decimals

```

Introduction
------------

[](#introduction)

Mathematical operations using PHP's built-in number types (float and integer) suffer from two drawbacks:

- limited range
- floating point inaccuracy

Whenever dealing with numbers out of the build-in number types' range or whenever floating point inaccuracy can become a problem (eg. currency calculations) an alternative solution is required.

In the same way as the BCMath extension, this library uses strings to represent numbers. Unlike PHP's float to string conversion (which uses a locale depended decimal separator), the decimal separator is always `"."`.

One thing you have to worry about when dealing with arbitrary precision mathematics is the precision you need. You have to tell how much precision you need for your results. Whenever possible, this library will do this for you. But for some operations you might have to manually set the required precision when you need extraordinary accuracy.

Usage
-----

[](#usage)

Any operations can be accessed using the static methods of the `Decimals` class:

```
Decimals::add('5.78', '7.1');

```

### Basic mathematical operations

[](#basic-mathematical-operations)

```
// add: 5.78 + 7.1
Decimals::add('5.78', '7.1');

// substract: 5.32 - 1.08
Decimals::sub('5.32', '1.08');

// multiply: 1.1 * 2.87
Decimals::mul('1.1', '2.87');

// divide: 5 / 2.5
Decimals::div('5', '2.5');

// modulus: 5 % 2
Decimals::mod('5', '2');

// pow: 5 ** 2
Decimals::pow('5', '2');

```

For `add()` and `sub()` the result precision is automatically set to the largest number of decimals of either operands. This is sufficient for any operation.

For `mul()`, `div()` and `mod()` the result precision is automatically set to the double of the largest number of decimals of either operands but at least 16 decimals. If you need higher precision, you may specify it as third parameter. The following example outputs 32 decimals:

```
Decimals::div('1', '3', 32);

```

The `sum()` function sums up all given operands:

```
Decimals::sum('1', '2.1', '-0.3');

```

### Converting and parsing

[](#converting-and-parsing)

Sometimes you need to convert a floating number to a string. As mentioned above, casting a floating number to a string uses the locale dependent decimal separator (e.g. "." for "en" and "," for "de"). This can cause a lot of problems when you have no control which locale your PHP interpreter uses (especially when writing libraries).

#### Convert native type to number string

[](#convert-native-type-to-number-string)

Luckily this library ships with a function, converting these local strings to the BCMath compatible number strings (with "." as decimal separator). You may simply pass your number to the `parse()` function:

```
$float = 1.5;

$sNumber = Decimals::parse($float);

```

**Always use the `parse()` function when converting native number types to string to write platform independent code!**

#### Parse string with any decimal separator

[](#parse-string-with-any-decimal-separator)

The `parse()` function is also useful when you have numbers from external sources with (maybe different) decimal separator than the locale. You can safely convert these strings by passing the decimal separator as second argument:

```
$sNumber = Decimals::parse('1,67', ',');

// $sNumber: '1.67'

```

The parse function also validates the input and throws an `InvalidArguemntException` if no a valid number is given. (Whitespaces around a valid number string are gracefully ignored and number is parsed without exception)

#### Convert number string to float or int

[](#convert-number-string-to-float-or-int)

In cases when you need to convert the value back to a native data type you can simply cast the string to the type or use the `toNative()` function which will return the correct type (float or int) automatically:

```
$float = Decimals::toNative('1.56');

$int = Decimals::toNative('167');

```

#### Normalize numbers

[](#normalize-numbers)

Often, a number can be represented in multiple forms:

- 0 can be expressed as "0.0", "0", "0." ".0", "-0", ...
- 5.8 can be expressed as "005.8", "5.8000", "+5.8" ...

The `norm()` function strips of any unneeded zeros before and after the number, remove any "+" signs and represent 0 always as "0":

```
Decimals::norm('+045.89');

```

### Rounding and truncating numbers

[](#rounding-and-truncating-numbers)

These functions are pretty self explaining. `round()` rounds the number to the given number of decimals, while `truncate()` simply strips of decimals:

```
Decimals::round('5.4591', 0); // = '5'
Decimals::round('5.4591', 1); // = '5.5'
Decimals::round('5.4591', 2); // = '5.46'

Decimals::truncate('5.4591', 0); // = '5'
Decimals::truncate('5.4591', 1); // = '5.4'
Decimals::truncate('5.4591', 2); // = '5.45'

```

### Comparison

[](#comparison)

To compare numbers, the `comp()` function can be used. It acts like the ``operator, returning `-1` if the left operand is lower, `0` if both operands are equal and `1` if the right operand is greater:

```
Decimals::comp('1.5', '6');   // = -1
Decimals::comp('1.5', '1.5'); // = 0
Decimals::comp('6', '1.5');   // = 1

```

To make code more readable, the following functions handle specific comparison cases:

```
Decimals::isEqual('1.5', '6');               // = false
Decimals::isNotEqual('1.5', '6');            // = true
Decimals::isGreaterThan('1.5', '6');         // = false
Decimals::isGreaterThanOrEqual('1.5', '6');  // = false
Decimals::isLessThan('1.5', '6');            // = true
Decimals::isLessThanOrEqual('1.5', '6');     // = true

```

To find the maximum or minimum of a given value set, the `max()` and `min()` functions exist:

```
Decimals::max('1', '2', '3');   // '3'
Decimals::min('1', '2', '3');   // '1'

```

### Mathematical functions

[](#mathematical-functions)

```
// returns the absolute value
Decimals::abs('-1.5'); // = '1.5'
Decimals::abs('1.5');  // = '1.5'

```

### Other functions

[](#other-functions)

```
// returns the number of decimals of the given number
Decimals::decimals('78.8');   // = 1
Decimals::decimals('78.889'); // = 3
Decimals::decimals('78.800'); // = 3

```

Using expressions
-----------------

[](#using-expressions)

Applying multiple operations can lead to unreadable code:

```
$result = Decimals::add(Decimals::mul($a, $b), $c);

```

The `Decimals::expr()` method (or the helper `expr()`) can help here. The same operations as above can be written as follows:

```
$result = Decimals::expr($a, '*', $b, '+', $c);

// or using helper
$result = expr($a, '*', $b, '+', $c);

```

This comes with the drawback of little performance overhead, but is very easy to read.

**Expressions are always evaluated from left to right.** If an expression is given which would break mathematical or logical operator precedence, an exception is thrown.

TODO
----

[](#todo)

- add wrapper for `bcpowmod()` and `bcsqrt()`

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity65

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 ~77 days

Recently: every ~160 days

Total

19

Last Release

1350d ago

Major Versions

1.4.0 → 2.0.02019-05-02

PHP version history (3 changes)2.0.0PHP ^7.0

2.4.0PHP ^7.2

2.5.2PHP &gt;=7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/44973729?v=4)[mehr.IT GmbH](/maintainers/mehr-it)[@mehr-it](https://github.com/mehr-it)

---

Top Contributors

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

---

Tags

decimalbcmathbc math

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mehr-it-php-decimals/health.svg)

```
[![Health](https://phpackages.com/badges/mehr-it-php-decimals/health.svg)](https://phpackages.com/packages/mehr-it-php-decimals)
```

###  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)[prestashop/decimal

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

178.2M5](/packages/prestashop-decimal)[rtlopez/decimal

An object oriented immutable arbitrary-precision arithmetic library for PHP

27262.8k2](/packages/rtlopez-decimal)[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)

PHPackages © 2026

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