PHPackages                             matmar10/lib-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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. matmar10/lib-money

ActiveLib[Validation &amp; Sanitization](/categories/validation)

matmar10/lib-money
==================

A series of money related utility classes wrapping integer math and money fractional allocation.

1.0.2(11y ago)1581[1 issues](https://github.com/matmar10/lib-money/issues)3Apache License Version 2.0PHP

Since Sep 21Pushed 9y ago2 watchersCompare

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

READMEChangelogDependencies (2)Versions (5)Used By (3)

Lib Money
=========

[](#lib-money)

[![Build Status](https://camo.githubusercontent.com/2fabfce7b6d2a57ebcfa13ff5c54e58aaba6aecd0a38535e8c0af47c55cb6111/687474703a2f2f63692e61736564696b2e636f6d2f6275696c645374617475732f69636f6e3f6a6f623d6d6f6e65792d62756e646c65)](http://ci.asedik.com/job/money-bundle/)

Overview
--------

[](#overview)

Symfony2 Bundle wrapping common Money and Currency related needs such as integer-based math, currency codes, and money conversion.

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

[](#installation)

Add the package to your composer.json file:

```
{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/matmar10/lib-money"
        }
    ],
    "require": {
        "matmar10/lib-money": "dev-master"
    }
}

```

Creating Objects
----------------

[](#creating-objects)

Currencies are identified by a currency code and have a calculation and display precision:

```
$eur = new Currency('EUR', 2, 2);

$euros = new Money($eur);
$euros->setAmountFloat(1.99);
```

Basic Math
----------

[](#basic-math)

The Money object wraps all basic math functions using underlying integer math to avoid the (problems with floating point math)\[\].

All amounts are stored as integer values internally using the calculation precision as the scale.

```
// must use a valid iso4217 currency code
// with the exception of BTC for Bitcoin, as specified in the override configuration
$usd = new Currency('USD', 5, 2);

$usdAmount1 = new Money($usd);
$usdAmount1->setAmountFloat(1.2345);

$usdAmount2 = new Money($usd);
$usdAmount2->setAmountFloat(1.2345);

$usdAmount1->isEqual($usdAmount2); // true

$resultAmount1 = $usdAmount1->add($usdAmount2);
echo $resultAmount1->getAmountDisplay(); // 2.47

$resultAmount2 = $usdAmount1->subtract($usdAmount2);
echo $resultAmount2->getAmountFloat(); // 0

$resultAmount3 = $usdAmount1->multiply(3);
echo $resultAmount3->getAmountFloat(); // 3.7035
echo $resultAmount3->getAmountDisplay(); // 3.70

$resultAmount4 = $usdAmount1->divide(2);
echo $resultAmount3->getAmountFloat(); // 0.61725
echo $resultAmount3->getAmountDisplay(); // 0.62
```

Dealing with Fractional Cents
-----------------------------

[](#dealing-with-fractional-cents)

How do you divide $10 evenly amongst three people? In reality, you can't divide fractional cents.

Really, you want to end up with three *equal*-ish shares:

- $3.34
- $3.33
- $3.33

```
$eurAmount = new Money(new Currency('EUR', 2, 2));
$eurAmount->setAmountFloat(10);

// split the 10 euros into three equal parts using euro cents as the smallest unit
$shares = $eurAmount->allocate(array(1, 1, 1), 2);

$shares[0]->getAmountFloat(); // 3.34
$shares[1]->getAmountFloat(); // 3.33
$shares[2]->getAmountFloat(); // 3.33
```

Converting Between Currencies
-----------------------------

[](#converting-between-currencies)

Use the `CurrencyPair` object to convert between disparate currencies using an exchange rate:

Note that the rate works bi-directionally:

```
$gbp = new Currency('GBP', 2, 2);
$usd = new Currency('USD', 2, 2);

$gbpAmount = new Money($gbp);
$gbpAmount->setAmountFloat(10);

// 1 GBP = 1.5 USD
$gbpToUsd = new CurrencyPair($gbp, $usd, 1.5);

$usdAmount = $gbpToUsd->convert($gbpAmount);
echo $usdAmount->getDisplay(); // 15.00

$gbpAmount2 = $gbpToUsd->convert($usdAmount);
echo $gbpAmount2->getDisplay(); // 10.00
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 63.6% 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 ~98 days

Total

4

Last Release

4321d ago

Major Versions

v0.0.1 → v1.0.02013-10-13

### Community

Maintainers

![](https://www.gravatar.com/avatar/9dede0530c32ff7421ef5cc42a19b9be4b8926111ae0bc3111993429a73c6b9b?d=identicon)[matmar10](/maintainers/matmar10)

---

Top Contributors

[![matmar10](https://avatars.githubusercontent.com/u/19975?v=4)](https://github.com/matmar10 "matmar10 (14 commits)")[![vvh-empora](https://avatars.githubusercontent.com/u/5236105?v=4)](https://github.com/vvh-empora "vvh-empora (8 commits)")

---

Tags

validatormoneycurrencyiso4217currency codeinteger math

### Embed Badge

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

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

###  Alternatives

[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M383](/packages/respect-validation)[seld/jsonlint

JSON Linter

1.3k217.8M205](/packages/seld-jsonlint)[composer/spdx-licenses

SPDX licenses list and validation library.

1.4k184.2M25](/packages/composer-spdx-licenses)[opis/json-schema

Json Schema Validator for PHP

64236.9M186](/packages/opis-json-schema)[laminas/laminas-validator

Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria

15544.9M188](/packages/laminas-laminas-validator)[ergebnis/json-schema-validator

Provides a JSON schema validator, building on top of justinrainbow/json-schema.

3626.9M7](/packages/ergebnis-json-schema-validator)

PHPackages © 2026

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