fortis/moneyobject
Composer 安装命令:
composer require fortis/moneyobject
包简介
A PHP library providing immutable Money value object
关键字:
README 文档
README
A PHP library providing immutable Money value object with arbitrary-precision and solution for floating point rounding errors.
What do you think will be printed in the example below?
print (36 - 35.99) === 0.01 ? '✅ equals' : 'not equals 😈';
Actually not equals 😈 . You can try https://ideone.com/2UQlBF.
Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.
-- Oracle
Install
Install directly from command line using Composer
composer require fortis/moneyobject
QuickStart
Currency code validation
$money = new Money(100, 'USF'); // throws InvalidCurrencyException
Create Money instance
$money = Money::USD(100.20); // 100.20 USD. Short syntax with autocomplete. $money = new Money(100.20, CurrencyCode::USD); // 100.20 USD $money = Money::create(100.20, CurrencyCode::USD); // 100.20 USD
Get currency
$money->getCurrency()->getCode(); // USD
Get amount
$money->getAmount()->toFloat(); // 100.20
Multiply: 100.20 * 2
$money->multiply(2) ->getAmount()->toFloat(); // 200.40
Divide: 100.20 / 2
$money->divide(Money::USD(2)) ->getAmount()->toFloat(); // 50.10
Plus: 100.20 + 2.5
$money->plus(Money::USD(2.5)) ->getAmount()->toFloat(); // 102.70
Minus: 100.20 - 0.5
$money->minus(Money::USD(0.5)) ->getAmount()->toFloat(); // 99.70
Minus: 36 - 35.99
Money::USD(36)->minus(Money::USD(35.99)) ->getAmount()->toFloat(); // 0.01
Minus: 36 - 35.99
Money::USD(36)->minus(35.99) ->getAmount()->toFloat(); // 0.01
Convert USD to EUR
$ composer require florianv/swap php-http/message php-http/guzzle6-adapter
$swap = (new SwapBuilder()) ->add('fixer') ->build(); $converter = new Converter($swap); $usd50 = Money::USD(50); $result = $converter->convert($usd50, Currency::EUR());
Credits
License
moneyobject is licensed under the MIT license.
统计信息
- 总下载量: 6.03k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-09-11