srtfisher / money
此包已被弃用且不再维护。未建议替换包。
PHP 实现Fowler的Money模式
1.3.0-beta1
2014-08-24 17:01 UTC
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+ 库,使金钱处理更安全、更简单、更有趣!
"如果每次我看到有人用FLOAT存储货币,我就有 $999.997634,我会有的" -- Bill Karwin
简而言之:你不应该用浮点数来表示货币值。无论何时需要表示金钱,请使用此Money值对象。
<?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();
文档可在 http://money.readthedocs.org 查找
货币格式
在创建货币对象时,它应该是最小货币单位(例如分)。例如,$1,234.56
实际上是 123,456
分。
从分转换为美元
<?php $dollars = $cents * 100;
从美元转换为分
<?php $cents = $dollars / 100;
安装
使用 composer 安装此库。将以下内容添加到你的 composer.json
{ "require": { "srtfisher/money": "dev-master" }, "minimum-stability": "dev" }
现在运行 install
命令。
$ composer.phar install
集成
查看 MoneyBundle
或 TbbcMoneyBundle
以获取 Symfony 集成。