货币 / 货币
Fowler的Money模式的PHP实现
2.0.3
2014-04-20 16:04 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: 3.7.*
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-08-29 03:29:20 UTC
README
从 Mathias Verraes实现Fowley的货币模式 分支。
当前分支版本通过在Money构造函数中使用浮点数作为参数,与Fowley模式有所不同。这是为了解决在32位平台上大货币实体超出PHP int大小的问题。这将在后续分支版本中解决,因此预期在次要版本上会有API破坏。
PHP 5.3+库,使处理货币更安全、更简单、更有趣!
"如果我每看到一次有人用FLOAT存储货币,我就有$999.997634" -- Bill Karwin
简而言之:你不应该用浮点数表示货币值。无论何时需要表示货币,请使用这个Money值对象。
<?php use Money\Money; $fiveEur = Money::EUR(5); $tenEur = $fiveEur->add($fiveEur); list($part1, $part2, $part3) = $tenEur->allocate(array(1, 1, 1)); assert($part1->equals(Money::EUR(334))); assert($part2->equals(Money::EUR(333))); assert($part3->equals(Money::EUR(333)));
文档可在 http://money.readthedocs.org 查看
安装
使用 composer 安装库。将以下内容添加到您的 composer.json
{ "require": { "hemeragestao/money": "2.0.*" } }
现在运行 install
命令。
$ composer.phar install
新增功能
此分支用Ruby的Money映射中的数据替换了来自openexchangerates.org的简单ISO 4217映射。这允许一些额外功能,包括为Money对象生成格式化的输出字符串。此外,Money对象构造函数接受一个浮点数参数,将其作为金额而不是单位使用。这将在后续版本中更改。
<?php use Money\Money; echo Money::EUR(5.32);
输出
€ 5.32
集成
有关Symfony集成的信息,请参阅MoneyBundle
或TbbcMoneyBundle
。