PHPackages                             rebilly/money - 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. rebilly/money

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

rebilly/money
=============

Implementation of the Money Value Object

v2.4.0(1y ago)6459.2k↓65.1%1[2 PRs](https://github.com/Rebilly/money/pulls)MITPHPPHP ^7.4 || ^8.0CI failing

Since Dec 8Pushed 4mo ago16 watchersCompare

[ Source](https://github.com/Rebilly/money)[ Packagist](https://packagist.org/packages/rebilly/money)[ Docs](https://github.com/rebilly/money/)[ RSS](/packages/rebilly-money/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (4)Versions (17)Used By (0)

[![Software License](https://camo.githubusercontent.com/a7e65aee57b11d28e4caff8b945729a66be0bb663f7f93bd24c5aa65699f148e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Latest Version on Packagist](https://camo.githubusercontent.com/44955db236260a286a5ac3f201c4741e9063465cc967135bf0eadd947d5e0fed/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f526562696c6c792f6d6f6e65792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Rebilly/money)[![GitHub Actions status](https://github.com/Rebilly/money/workflows/Tests/badge.svg)](https://github.com/Rebilly/money)

Money
=====

[](#money)

[Value Object](http://martinfowler.com/bliki/ValueObject.html) that represents a [monetary value using a currency's smallest unit](http://martinfowler.com/eaaCatalog/money.html). This library is originally based on Sebastian Bergmann's [discontinued library](https://github.com/sebastianbergmann/money). It's been updated with support added for currency exchange.

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

[](#installation)

Simply add a dependency on `rebilly/money` to your project's `composer.json` file if you use [Composer](https://getcomposer.org/) to manage the dependencies of your project.

Usage Examples
--------------

[](#usage-examples)

#### Creating a Money object and accessing its monetary value

[](#creating-a-money-object-and-accessing-its-monetary-value)

```
use Money\Currency;
use Money\Money;

// Create Money object that represents 1 EUR
$m = new Money(100, new Currency('EUR'));

// Access the Money object's monetary value
print $m->getAmount();

// Access the Money object's monetary value converted to its base units
print $m->getConvertedAmount();
```

The code above produces the output shown below:

```
100

1.00

```

#### Creating a Money object from a string value

[](#creating-a-money-object-from-a-string-value)

```
use Money\Currency;
use Money\Money;

// Create Money object that represents 12.34 EUR
$m = Money::fromString('12.34', new Currency('EUR'))

// Access the Money object's monetary value
print $m->getAmount();
```

The code above produces the output shown below:

```
1234

```

#### Basic arithmetic using Money objects

[](#basic-arithmetic-using-money-objects)

```
use Money\Currency;
use Money\Money;

// Create two Money objects that represent 1 EUR and 2 EUR, respectively
$a = new Money(100, new Currency('EUR'));
$b = new Money(200, new Currency('EUR'));

// Negate a Money object
$c = $a->negate();
print $c->getAmount();

// Calculate the sum of two Money objects
$c = $a->add($b);
print $c->getAmount();

// Calculate the difference of two Money objects
$c = $b->subtract($a);
print $c->getAmount();

// Multiply a Money object with a factor
$c = $a->multiply(2);
print $c->getAmount();
```

The code above produces the output shown below:

```
-100
300
100
200

```

The `compareTo()` method returns an integer less than, equal to, or greater than zero if the value of one `Money` object is considered to be respectively less than, equal to, or greater than that of another `Money` object.

You can use the `compareTo()` method to sort an array of `Money` objects using PHP's built-in sorting functions:

```
use Money\Currency;
use Money\Money;

$m = array(
    new Money(300, new Currency('EUR')),
    new Money(100, new Currency('EUR')),
    new Money(200, new Currency('EUR'))
);

usort(
    $m,
    function ($a, $b) { return $a->compareTo($b); }
);

foreach ($m as $_m) {
    print $_m->getAmount() . "\n";
}
```

The code above produces the output shown below:

```
100
200
300

```

#### Allocate the monetary value represented by a Money object among N targets

[](#allocate-the-monetary-value-represented-by-a-money-object-among-n-targets)

```
use Money\Currency;
use Money\Money;

// Create a Money object that represents 0,99 EUR
$a = new Money(99, new Currency('EUR'));

foreach ($a->allocateToTargets(10) as $t) {
    print $t->getAmount() . "\n";
}
```

The code above produces the output shown below:

```
10
10
10
10
10
10
10
10
10
9

```

#### Allocate the monetary value represented by a Money object using a list of ratios

[](#allocate-the-monetary-value-represented-by-a-money-object-using-a-list-of-ratios)

```
use Money\Currency;
use Money\Money;

// Create a Money object that represents 0,05 EUR
$a = new Money(5, new Currency('EUR'));

foreach ($a->allocateByRatios(array(3, 7)) as $t) {
    print $t->getAmount() . "\n";
}
```

The code above produces the output shown below:

```
2
3

```

#### Extract a percentage (and a subtotal) from the monetary value represented by a Money object

[](#extract-a-percentage-and-a-subtotal-from-the-monetary-value-represented-by-a-money-object)

```
use Money\Currency;
use Money\Money;

// Create a Money object that represents 100,00 EUR
$original = new Money(10000, new Currency('EUR'));

// Extract 21% (and the corresponding subtotal)
$extract = $original->extractPercentage(21);

printf(
    "%d = %d + %d\n",
    $original->getAmount(),
    $extract['subtotal']->getAmount(),
    $extract['percentage']->getAmount()
);
```

The code above produces the output shown below:

```
10000 = 8265 + 1735

```

Please note that this extracts the percentage out of a monetary value where the percentage is already included. If you want to get the percentage of the monetary value you should use multiplication (`multiply(0.21)`, for instance, to calculate 21% of a monetary value represented by a Money object) instead.

#### Convert Money object currency to to another Money object given a conversion rate

[](#convert-money-object-currency-to-to-another-money-object-given-a-conversion-rate)

```
use Money\Currency;
use Money\Money;

// Create a Money object that represents 100,00 EUR
$original = new Money(10000, new Currency('EUR'));

$converted = $original->convert(new Currency('USD'), 1.09, PHP_ROUND_HALF_UP);
$converted->getConvertedAmount(); // 109.00
```

#### Convert Money using a Rate Object

[](#convert-money-using-a-rate-object)

```
$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));
$rate = new Rate($cp, new DateTime(), 0.92);

$money = new Money(1000, new Currency('USD'));
$markupBips = 500; // 5% markup

$newMoney = $rate->convert($money)->multiply($markupBips / 10000 + 1);
```

#### Get Currency Exchange Rate Using a Rate Provider

[](#get-currency-exchange-rate-using-a-rate-provider)

```
$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));
$rateProvider = new InMemoryRateProvider(['EUR/USD' => 1.09, 'USD/EUR' => 0.9172], new DateTime());
$rate = $c->getRate($rateProvider);
```

Tests
-----

[](#tests)

```
phpunit

```

Security
--------

[](#security)

If you discover a security vulnerability, please report it to security at rebilly dot com.

License
-------

[](#license)

The Money library is open-sourced under the [MIT License](./LICENSE) distributed with the software.

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance63

Regular maintenance activity

Popularity40

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor3

3 contributors hold 50%+ of commits

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

Recently: every ~290 days

Total

14

Last Release

419d ago

Major Versions

v1.2.2 → v2.0.02022-02-09

PHP version history (4 changes)v1.0.0PHP ^7.1

v1.1.0PHP ^7.3

v1.2.0PHP ^7.3 || ^8.0

v2.0.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1161871?v=4)[Adam Altman](/maintainers/adamaltman)[@adamaltman](https://github.com/adamaltman)

---

Top Contributors

[![slavcodev](https://avatars.githubusercontent.com/u/757721?v=4)](https://github.com/slavcodev "slavcodev (8 commits)")[![adamaltman](https://avatars.githubusercontent.com/u/1161871?v=4)](https://github.com/adamaltman "adamaltman (3 commits)")[![ninjasimon](https://avatars.githubusercontent.com/u/7696653?v=4)](https://github.com/ninjasimon "ninjasimon (3 commits)")[![ashkarpetin](https://avatars.githubusercontent.com/u/400534?v=4)](https://github.com/ashkarpetin "ashkarpetin (2 commits)")[![agalstian](https://avatars.githubusercontent.com/u/55399628?v=4)](https://github.com/agalstian "agalstian (2 commits)")[![2e3s](https://avatars.githubusercontent.com/u/4003445?v=4)](https://github.com/2e3s "2e3s (2 commits)")[![VladimirRebilly](https://avatars.githubusercontent.com/u/144002104?v=4)](https://github.com/VladimirRebilly "VladimirRebilly (1 commits)")[![akozhevnikov-rebilly](https://avatars.githubusercontent.com/u/91593013?v=4)](https://github.com/akozhevnikov-rebilly "akozhevnikov-rebilly (1 commits)")[![colinbird](https://avatars.githubusercontent.com/u/3696815?v=4)](https://github.com/colinbird "colinbird (1 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![i-yasko](https://avatars.githubusercontent.com/u/121119455?v=4)](https://github.com/i-yasko "i-yasko (1 commits)")[![vgoncearencu](https://avatars.githubusercontent.com/u/9070145?v=4)](https://github.com/vgoncearencu "vgoncearencu (1 commits)")

---

Tags

moneycurrency

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rebilly-money/health.svg)

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

###  Alternatives

[brick/money

Money and currency library

1.9k40.4M139](/packages/brick-money)[florianv/swap

PHP currency conversion library for retrieving exchange rates from 30 providers, with caching and fallback.

1.3k6.7M22](/packages/florianv-swap)[cknow/laravel-money

Laravel Money

1.0k4.6M29](/packages/cknow-laravel-money)[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7855.7M42](/packages/akaunting-laravel-money)[kwn/number-to-words

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

4275.3M23](/packages/kwn-number-to-words)[torann/currency

This provides Laravel with currency functions such as currency formatting and conversion using up-to-date exchange rates.

4031.1M6](/packages/torann-currency)

PHPackages © 2026

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