srtfisher / money
PHP implementation of Fowler's Money pattern
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 4.*
Suggests
- Sylius/SyliusMoneyBundle: Sylius' Symfony2 integration with Money library
- TheBigBrainsCompany/TbbcMoneyBundle: Very complete Symfony2 bundle with support for Twig, Doctrine, Forms, ...
- pink-tie/money-bundle: Pink-Tie's Symfony2 integration with Money library
This package is auto-updated.
Last update: 2024-05-07 13:55:43 UTC
README
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.