srtfisher/money

PHP implementation of Fowler's Money pattern

1.3.0-beta1 2014-08-24 17:01 UTC

README

Build Status

PHP 5.4+ library to make working with money safer, easier, and fun!

"If I had a dime for every time I've seen someone use FLOAT to store currency, I'd have $999.997634" -- Bill Karwin

In short: You shouldn't represent monetary values by a float. Wherever you need to represent money, use this Money value object.

<?php

use Money\Money;

$fiveEur = Money::Euro(500);
$tenEur = $fiveEur->add($fiveEur);

list($part1, $part2, $part3) = $tenEur->allocate(array(1, 1, 1));
assert($part1->equals(Money::Euro(334)));
assert($part2->equals(Money::Euro(333)));
assert($part3->equals(Money::Euro(333)));

// Format
// $1,234.56
echo Money::USD(123456)->format();

The documentation is available at http://money.readthedocs.org

Money Format

When creating a money object, it is expected to be in the lowest value of the currency (e.g. cents). For example, $1,234.56 is actually 123,456 cents.

For conversion from cents to dollars:

<?php
$dollars = $cents * 100;

For conversion from dollars to cents:

<?php
$cents = $dollars / 100;

Installation

Install the library using composer. Add the following to your composer.json:

{
    "require": {
      "srtfisher/money": "dev-master"
    },
    "minimum-stability": "dev"
}

Now run the install command.

$ composer.phar install

Integration

See MoneyBundle or TbbcMoneyBundle for Symfony integration.