tiny-blocks / money
表示货币价值的值对象。
2.0.1
2023-06-17 15:47 UTC
Requires
- php: ^8.1||^8.2
- tiny-blocks/currency: ^2.0
- tiny-blocks/math: ^2.0
- tiny-blocks/value-object: ^2.0
Requires (Dev)
- infection/infection: ^0.26
- phpmd/phpmd: ^2.13
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.7
Suggests
- ext-bcmath: Enables the extension which is an interface to the GNU implementation as a Basic Calculator utility library.
README
概览
表示货币价值的值对象。
安装
composer require tiny-blocks/money
如何使用
该库提供了表示和执行货币操作的具体系列实现。
使用 from 方法
使用 from
方法,可以从有效的数值创建一个新的 Money
类型实例。您可以提供一个 string
、float
或 BigNumber
值。
Money::from(value: 10, currency: 'BRL'); Money::from(value: '10', currency: Currency::USD); Money::from(value: BigDecimal::from(value: '10'), currency: Currency::USD);
从 float
实例化的浮点值可能不安全,因为它们设计上是不精确的,可能会导致精度损失。始终建议从 string
实例化,它支持无限数量的数字。
使用数学运算方法
加法
执行此值与另一个值之间的加法运算。
$augend = Money::from(value: '100', currency: 'BRL'); $addend = Money::from(value: '1.50', currency: Currency::BRL); $result = $augend->add(addend: $addend); $result->amount->toString(); # 101.50
减法
执行此值与另一个值之间的减法运算。
$minuend = Money::from(value: '10.50', currency: 'EUR'); $subtrahend = Money::from(value: '0.50', currency: Currency::EUR); $result = $minuend->subtract(subtrahend: $subtrahend); $result->amount->toString(); # 10.00
乘法
执行此值与另一个值之间的乘法运算。
$multiplicand = Money::from(value: '5', currency: 'GBP'); $multiplier = Money::from(value: '3.12', currency: Currency::GBP); $result = $multiplicand->multiply(multiplier: $multiplier); $result->amount->toString(); # 15.60
除法
执行此值与另一个值之间的除法运算。
$dividend = Money::from(value: '8.99', currency: 'CHF'); $divisor = Money::from(value: '5', currency: Currency::CHF); $result = $dividend->divide(divisor: $divisor); $result->amount->toString(); # 1.79
许可证
Money 采用 MIT 许可证。
贡献
请遵循 贡献指南 为项目做出贡献。