PHPackages                             happy-types/money-type - 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. happy-types/money-type

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

happy-types/money-type
======================

Money library implements Money Value Object which contains amount and currency in the same object.

v1.2.2(7y ago)026.6k↓53.2%2MITPHPPHP &gt;=5.2.0CI failing

Since Nov 7Pushed 6y ago1 watchersCompare

[ Source](https://github.com/antanas-arvasevicius/php-money-type)[ Packagist](https://packagist.org/packages/happy-types/money-type)[ Docs](https://github.com/antanas-arvasevicius/php-money-type)[ RSS](/packages/happy-types-money-type/feed)WikiDiscussions master Synced yesterday

READMEChangelog (3)DependenciesVersions (5)Used By (0)

Money library
=============

[](#money-library)

This library adds a new type **Money** which abstracts a real money object. **Money** has an **Value** and **Currency**. Without **Money** object you must store these values in separate variables like $amount, $currency. Its really hard to produce nice API methods without money object.

Example:

```
function getPrice()
```

Should it return value or object ?

```
function getPrice()
{
   return array($price, $value);
}
```

or better?

```
function getPrice()
{
   return Money::create($price, $value);
}
```

now you can apply some mathematics to money object:

```
getPrice()->add(Money::create('10.00', 'LTL'))
```

or add and convert to some currency in same time

```
$priceInUsd = getPrice()->add(Money::create('10.00', 'LTL'))->convertTo('USD')
```

or with mutiple currencies ? wha?

```
$sum = Money::create('10.00', 'LTL')->add(Money::create('5.00', 'USD'))

echo $sum->asString(); // 24.0000 LTL
```

comparing moneys with different currencies ?

```
Money::create('10.00', 'LTL')->compare(Money::create('100.00', 'RUB'));
```

To enable currency conversion you must implement `MoneyConverter` interface and register it to `Money` using: `Money::setDefaultMoneyConverter()`

Implementation example of `MoneyConverter` can be found at `/tests/MockedMoneyConverter.php`

Money
-----

[](#money)

Class Money is a Value Object which means that all operations will create new instance of Money and all instances are immutable

This class is used in all system to describe money value. It is {amount, currency} tuple.

Methods `add()`, `subtract()`, `compare()`, `isEqual()` will implicitly convert specified argument into appropriate currency.

For currency conversion you can implement `MoneyConverter` interface and register your money converter using `Money::setDefaultMoneyConverter()`

Default decimal precision is 4 digits

You can compare only two objects with exact the same precisions. All operations will use highest precisions of available operands.

use `Money::create($amount = '', $currency = '', $precision = -1)` static factory to create Money instances. **don`t use constructors directly!**

amount must be specified in string type and decimal separator must be a dot (e.g. "123.45") You cannot specify `Money::create(123.45)` or `Money::create("123,45")`. only `Money::create("123.45")` &lt;- string, and dot separated number

Money amount **must be** string type only. Number **must be** dot separated. e.g. "123.45" not "123,45" (bc limitation) You **cannot** use float or integer types. Its defensive restriction to ensure that you wont loose precision accidentaly. In case if you want to specify integers or floats or string you can use ```Money::createUnsafe()`` method. but its **not recommended**.

use `Money::undefined($currency = '', $precision = -1)` to create value with unknown money amount. its syntactic sugar for `Money::create(false)`, `Money::create(null)` or `Money::create('')`

```
e.g. Money::create('') === Money::undefined();
   Money::create('0.00') !== Money::undefined();

```

you can use methods `isDefined()` `isUndefined()` to check whether Money object has an defined amount.

all undefined values in any money operations are allowed and will be casted to zero

Note: please remind that currency conversions will accumulate computation errors, so operations like:

```
a = Money::create('10.00', 'LTL')
b = a->convertTo('USD')->convertTo('LVL')->convertTo('LTL');

```

will produce value which was not equal to previous value due conversion errors. absolute error is +-0.0003

you can compare these values with `isEqualExact()` with lower precision

Mutable Money
-------------

[](#mutable-money)

Class MutableMoney

This class is the same as Money class except that this class is not Value Object so it doesn't create new instances but operations are applied to the same instances. so `add()` and `subtract()` methods will affect instance values.

Uses this class for intensive computations of lot of values

Default System Currency
-----------------------

[](#default-system-currency)

As of v1.1 you can specify default currency which will be passed to all new **Money** object as a default currency if not specified. See `Money::setDefaultCurrency($defaultCurrency)`

Database
--------

[](#database)

To persist this object in database please use **DECIMAL(12, 4)** for amount, and **CHAR(3) BINARY** for currency!

Installation and usage
----------------------

[](#installation-and-usage)

add these lines to your `composer.json`

```
    "require":
    {
        "happy-types/money-type" : "~1.1"
    }
}

```

run: `composer update`

insert composer auto loader in your project: `require_once(dirname(__FILE__).'/vendor/autoload.php');`

example is for index.php which is in project root folder.

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 60% 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 ~87 days

Total

4

Last Release

2896d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ae6fb9743d669bdd92c4216bdabf16ced85ef550b454ec6ceb4cc991c55b9aaf?d=identicon)[antanas\_a](/maintainers/antanas_a)

---

Top Contributors

[![Bajoras](https://avatars.githubusercontent.com/u/9000752?v=4)](https://github.com/Bajoras "Bajoras (3 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (2 commits)")

---

Tags

moneyquantityMoney Convert

### Embed Badge

![Health badge](/badges/happy-types-money-type/health.svg)

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

###  Alternatives

[moneyphp/money

PHP implementation of Fowler's Money pattern

4.8k91.5M524](/packages/moneyphp-money)[brick/money

Money and currency library

1.9k41.8M157](/packages/brick-money)[florianv/swap

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

1.3k6.8M23](/packages/florianv-swap)[cknow/laravel-money

Laravel Money

1.0k4.8M31](/packages/cknow-laravel-money)[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7865.8M47](/packages/akaunting-laravel-money)[kwn/number-to-words

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

4375.4M23](/packages/kwn-number-to-words)

PHPackages © 2026

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