PHPackages                             byrokrat/amount - 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. byrokrat/amount

Abandoned → [moneyphp/money](/?search=moneyphp%2Fmoney)ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

byrokrat/amount
===============

Value objects for monetary amounts

2.1.0(6y ago)48.4k12UnlicensePHPPHP &gt;=7.0

Since Jan 26Pushed 6y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (6)Used By (2)

> ABANDONED! This package is discontinued and will not be updated. See [moneyphp/money](https://packagist.org/packages/moneyphp/money) instead.

Amount
======

[](#amount)

[![Packagist Version](https://camo.githubusercontent.com/09870bf255b580525c409094df97f2fa57e0d33144a8b9381e7ea87d19b73b4d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6279726f6b7261742f616d6f756e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/byrokrat/amount)[![Build Status](https://camo.githubusercontent.com/5982d77f90291238b41b7c60a33f35785db1887629ab5f00e1e21b89a9240584/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6279726f6b7261742f616d6f756e742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/byrokrat/amount)[![Quality Score](https://camo.githubusercontent.com/9c6b95dbdb01fd375faeeff175083fd83172eb33f3615743ca1836a2a4d07153/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6279726f6b7261742f616d6f756e742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/byrokrat/amount)[![Scrutinizer Coverage](https://camo.githubusercontent.com/44d8e2bf692b9ed7eb39012c014f274755b5526d1e628573ee719488cf5aedcc/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6279726f6b7261742f616d6f756e742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/byrokrat/amount/?branch=master)

Value objects for monetary amounts.

Features
--------

[](#features)

- Immutable value object.
- Using the [bcmath](http://php.net/manual/en/book.bc.php) extension for arbitrary floating point arithmetic precision.
- [Currency](#working-with-currencies) support to prevent mixing currencies.
- Out of the box support for ISO 4217 currencies.
- Simple interface for [defining new currencies](#creating-new-currencies).
- Support for multiple [rounding](#rounding) strategies.
- Support for [allocating](#allocating) amounts based on ratios.
- Support for the [signal string](#signal-strings) format as used in the swedish direct debit system.

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

[](#installation)

```
composer require byrokrat/amount
```

Usage
-----

[](#usage)

```
use byrokrat\amount\Amount;

$amount = new Amount('100.6');

// outputs 1 (true)
echo $amount->isGreaterThan(new Amount('50'));

// round to 0 decimal digits
$roundedAmount = $amount->roundTo(0);

// outputs 101.00
echo $roundedAmount;
```

API
---

[](#api)

[`Amount`](/src/Amount.php) defines the following api:

Method signaturereturnsdescription**\_\_construct(string $amount)**AmountCreate new instance**getAmount()**stringGet raw amount**roundTo(\[int $precision, \[Rounder $rounder\]\])**AmountGet new Amount rounded to $precision**getString(\[int $precision, \[Rounder $rounder\]\])**stringGet amount as string**\_\_tostring()**stringGet amount as string**getInt(\[Rounder $rounder\])**integerGet amount as integer (WARNING)**getFloat(\[int $precision, \[Rounder $rounder\]\])**floatGet amount as float (WARNING)**getSignalString(\[Rounder $rounder\])**stringGet amount as a signal string**add(Amount $amount)**AmountGet new Amount with $amount added**subtract(Amount $amount)**AmountGet new Amount with $amount subtracted**multiplyWith(mixed $amount)**AmountGet new Amount multiplied with $amount**divideBy(mixed $amount)**AmountGet new Amount divided by $amount**compareTo(Amount $amount)**integer0 if equals, 1 if greater, -1 otherwise**equals(Amount $amount)**booleanCheck if equals amount**isLessThan(Amount $amount)**booleanCheck if less than amount**isLessThanOrEquals(Amount $amount)**booleanCheck if less than or equals amount**isGreaterThan(Amount $amount)**booleanCheck if greater than amount**isGreaterThanOrEquals(Amount $amount)**booleanCheck if greater than or equals amount**isZero()**booleanCheck if amount is zero**isPositive()**booleanCheck if amount is greater than zero**isNegative()**booleanCheck if amount is less than zero**getInverted()**AmountGet new amount with sign inverted**getAbsolute()**AmountGet new amount with negative sign removed**allocate(array $ratios, \[int $precision\])**Amount\[\]Allocate amount based on list of ratiosCreating Amounts from other formats
-----------------------------------

[](#creating-amounts-from-other-formats)

### Floating point numbers

[](#floating-point-numbers)

`Amount` contains two convenience methods for working with floating point numbers. `createFromNumber` can create an Amount object from a floating point number, `getFloat` can convert an Amount object to a float. These methods should be used with care.

It is important to note that computers internally use the binary floating point format and cannot accurately represent a number like 0.1, 0.2 or 0.3 at all. Using floating point numbers leads to a loss of precision. For example `floor((0.1+0.7)*10)` will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

For this reason floats should never ne used to store monetary data. These methods exists for rare situations when converting to or from native formats is inevitable. Unless you know what you are doing they should **NOT** be used.

For more information see the [php manual](http://php.net/manual/en/language.types.float.php)or [What Every Programmer Should Know About Floating-Point Arithmetic](http://floating-point-gui.de/).

### Formatted numbers

[](#formatted-numbers)

You can create Amounts from strings formatted with non-standard or locale dependent decimal point and grouping characters using the static method **createFromFormat**.

```
use byrokrat\amount\Amount;

$formattedAmount = "2 000:50";

$amount = Amount::createFromFormat($formattedAmount, ":", " ");

echo $amount;  // outputs 2000.50
```

### Signal strings

[](#signal-strings)

The signal string format contans no decimal point and negative amounts are signaled using a letter instead of the final digit. Create Amounts from signal strings using the static method **createFromSignalString**.

Working with currencies
-----------------------

[](#working-with-currencies)

The currency subsystem helps prevent bugs where values in different currencies are mixed (for example added together). Currency objects subclass `Amount` and works in the same way, with the added feature that they know their defined currency.

```
use byrokrat\amount\Currency\SEK;
use byrokrat\amount\Currency\EUR;

$sek = new SEK('100');

// throws an exception
$sek->add(new EUR('1'));
```

```
use byrokrat\amount\Currency\SEK;

$sek = new SEK('100');

// works as intended, outputs 101.00
echo $sek->add(new SEK('1'));
```

### ISO 4217

[](#iso-4217)

A comprehensive set of ISO 4217 currencies are bundled in the [`byrokrat\amount\Currency`](/src/Currency/) namespace.

Please note that the Turkish Lira with the ISO code `TRY` is represented as `_TRY` as try is a reserved keyword.

### Creating new currencies

[](#creating-new-currencies)

Creating new currencies however is straight forward. Simply subclass the [`Currency`](/src/Currency.php) class and define `getCurrencyCode()`.

Additionaly you may override `getDisplayPrecision()`, `getInternalPrecision()` and `getDefaultRounder()` to further define your currency's behaviour.

### Exchanging

[](#exchanging)

Exchanging currencies is supported using `createFromExchange`. Note that you must supply the correct exchange rate.

```
use byrokrat\amount\Currency\SEK;
use byrokrat\amount\Currency\EUR;

// One euro is exchanged into swedish kronas using the exchange rate 9.27198929
// resulting in the value of SEK 9.27198929
echo $sek = SEK::createFromExchange(new EUR('1'), '9.27198929');
```

### Formatting currencies

[](#formatting-currencies)

Currency objects can easily be formatted using php's built in `NumberFormatter`.

```
use byrokrat\amount\Currency\EUR;

// Create some amount of euros
$money = new EUR('1234567.89');

// Create a currency formatter with swedish formatting rules
$formatter = new NumberFormatter('sv_SE', NumberFormatter::CURRENCY);

// Format euros according to swedish standards, outputs 1 234 567:89 €
echo $formatter->formatCurrency($money->getFloat(), $money->getCurrencyCode());
```

Rounding
--------

[](#rounding)

A number of rounding strategies are supported. To implement your own see the [Rounder](/src/Rounder.php) interface.

```
namespace byrokrat\amount;
$amount = new Amount('1.5');

// outputs 2
echo $amount->roundTo(0, new Rounder\RoundUp);

// outputs 1
echo $amount->roundTo(0, new Rounder\RoundDown);

// outputs 1
echo $amount->roundTo(0, new Rounder\RoundTowardsZero);

// outputs 2
echo $amount->roundTo(0, new Rounder\RoundAwayFromZero);

// outputs 2
echo $amount->roundTo(0, new Rounder\RoundHalfUp);

// outputs 1
echo $amount->roundTo(0, new Rounder\RoundHalfDown);

// outputs 1
echo $amount->roundTo(0, new Rounder\RoundHalfTowardsZero);

// outputs 2
echo $amount->roundTo(0, new Rounder\RoundHalfAwayFromZero);

// outputs 2
echo $amount->roundTo(0, new Rounder\RoundHalfToEven);

// outputs 1
echo $amount->roundTo(0, new Rounder\RoundHalfToOdd);
```

For more info on rounding strategies see [wikipedia](https://en.wikipedia.org/wiki/Rounding).

Allocating
----------

[](#allocating)

Allocating is the process of dividing an amount based on ratios in such a way that the smallest unit is not divided (every currency has a smallest unit that is non-dividable) but instead handed to the receiver next in line.

The ratios can be seen as (but does not have to be) percentages. A hundred units can thous be divided to two receivers as

```
use byrokrat\amount\Amount;
$money = new Amount('100');

list($receiverA, $receiverB) = $money->allocate([30, 70]);

// outputs 30
echo $receiverA;

// outputs 70
echo $receiverB;
```

The strength of allocating becomes clear when we distribute a value that we are not able to divide evenly. In this case the order of the receivers is significant.

```
use byrokrat\amount\Amount;

$money = new Amount('0.05');

list($receiverA, $receiverB) = $money->allocate([30, 70]);

// outputs 0.03
echo $receiverA;

// outputs 0.02
echo $receiverB;

list($receiverA, $receiverB) = $money->allocate([70, 30]);

// outputs 0.04
echo $receiverA;

// outputs 0.01
echo $receiverB;
```

In these examples the undividable unit used is `0.01`. This is the default behaviour. Change it either by specifying the `$precision` parameter or by overriding `getDisplayPrecision()` in your currency class.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity62

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

Total

5

Last Release

2461d ago

Major Versions

1.0.2 → 2.0.02017-12-06

PHP version history (2 changes)1.0.0PHP &gt;=5.4

2.0.0PHP &gt;=7.0

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

currencymoneymoneycurrencymonetary amounteconomics

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/byrokrat-amount/health.svg)

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

###  Alternatives

[brick/money

Money and currency library

1.9k37.9M102](/packages/brick-money)[florianv/swap

Exchange rates library for PHP

1.3k6.4M16](/packages/florianv-swap)[cknow/laravel-money

Laravel Money

1.0k4.3M22](/packages/cknow-laravel-money)[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7825.3M18](/packages/akaunting-laravel-money)[kwn/number-to-words

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

4235.0M21](/packages/kwn-number-to-words)[florianv/laravel-swap

Currency exchange rates library for Laravel and Lumen

3342.0M2](/packages/florianv-laravel-swap)

PHPackages © 2026

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