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

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

zolotar/money
=============

Value Object that represents a monetary value (using a currency's smallest unit)

0266PHP

Since Feb 4Pushed 10y ago1 watchersCompare

[ Source](https://github.com/Zolotar1988/money)[ Packagist](https://packagist.org/packages/zolotar/money)[ RSS](/packages/zolotar-money/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/0961ac5f6f16b64d1bb80e8d21a6d216ceb06e44a4f13b4724fcf4f96f073bd3/68747470733a2f2f706f7365722e707567782e6f72672f73656261737469616e2f6d6f6e65792f762f737461626c652e706e67)](https://packagist.org/packages/sebastian/money)[![Build Status](https://camo.githubusercontent.com/c128b48d1df6714a627255b3f4285086daf65b68387455bf2291b26eb878796d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73656261737469616e626572676d616e6e2f6d6f6e65792f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sebastianbergmann/money/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/3245444316d6a3e2e0557b1b4d26f3a777b463157224968725622efab513eab6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73656261737469616e626572676d616e6e2f6d6f6e65792f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sebastianbergmann/money/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/a0609477f86a95d1786491346de67d87ba0e3dded2388546a273d839e1fe07fc/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73656261737469616e626572676d616e6e2f6d6f6e65792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sebastianbergmann/money/?branch=master)[![Reference Status](https://camo.githubusercontent.com/c70961a360e72252adc8a5ad5819b492bcd7b6a6f2bdb341bff2bd0b698fdb4b/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f7068702f73656261737469616e3a6d6f6e65792f7265666572656e63655f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/php/sebastian:money/references)

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).

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

[](#installation)

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

Here is a minimal example of a `composer.json` file that just defines a dependency on Money:

```
{
    "require": {
        "sebastian/money": "1.5.*"
    }
}

```

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

[](#usage-examples)

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

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

```
use SebastianBergmann\Money\Currency;
use SebastianBergmann\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 SebastianBergmann\Money\Currency;
use SebastianBergmann\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

```

#### Using a Currency-specific subclass of Money

[](#using-a-currency-specific-subclass-of-money)

```
use SebastianBergmann\Money\EUR;

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

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

The code above produces the output shown below:

```
100

```

Please note that there is no subclass of `Money` that is specific to Turkish Lira as `TRY` is not a valid class name in PHP.

#### Basic arithmetic using Money objects

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

```
use SebastianBergmann\Money\Currency;
use SebastianBergmann\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

```

#### Comparing Money objects

[](#comparing-money-objects)

```
use SebastianBergmann\Money\Currency;
use SebastianBergmann\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'));

var_dump($a->lessThan($b));
var_dump($a->greaterThan($b));

var_dump($b->lessThan($a));
var_dump($b->greaterThan($a));

var_dump($a->compareTo($b));
var_dump($a->compareTo($a));
var_dump($b->compareTo($a));
```

The code above produces the output shown below:

```
bool(true)
bool(false)
bool(false)
bool(true)
int(-1)
int(0)
int(1)

```

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 SebastianBergmann\Money\Currency;
use SebastianBergmann\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 SebastianBergmann\Money\Currency;
use SebastianBergmann\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 SebastianBergmann\Money\Currency;
use SebastianBergmann\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 SebastianBergmann\Money\Currency;
use SebastianBergmann\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.

money
=====

[](#money-1)

###  Health Score

22

—

LowBetter than 23% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/a0912084121ea2e2483c3fdb56a7f20ecee37226bbd70e467bcfa948bf87e53a?d=identicon)[Zolotar1988](/maintainers/Zolotar1988)

---

Top Contributors

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

### Embed Badge

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

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

###  Alternatives

[whitehat101/apr1-md5

Apache's APR1-MD5 algorithm in pure PHP

349.7M10](/packages/whitehat101-apr1-md5)

PHPackages © 2026

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