ferdiunal / money
为PHP提供简单而实用的货币操作。
v1.0.4
2023-04-08 23:17 UTC
Requires
- php: >=8.1
README
本包是sineld/money存储库的分支,增加了折扣和数据类型。
ferdiunal/money是一个PHP库,旨在使处理货币变得更容易!没有静态属性或方法。传递给类的任何数字都将自动准备进行数学运算。该类使用 , 作为千位分隔符,. 作为小数点。
本包可以与任何框架或意大利面应用一起使用。如果您遇到任何问题,请通过电子邮件与我联系。
安装
通过Composer
$ composer require ferdiunal/money
将以下use语句添加到文件顶部
use Ferdiunal\Money\Money;
然后,开始使用库!
非Composer用户
如果您不使用Composer,只需将位于src文件夹中的Money.php文件复制到您的项目中,然后开始使用库。没有额外的依赖项。
请求方法别名
以下是与方法一起使用的参数
money->setDecimals(默认=2)
money->addTax(默认=18)
money->removeTax(默认=18)
money->setLocaleActive(默认=false)
money->setLocaleCode(默认=TRL)
money->setLocalePosition(默认=prefix, (使用 "suffix" 替代 reverse))
用法示例
在以下代码示例中,使用Ferdiunal\Money\Money
类创建了一个货币对象。首先,基于指定的数值创建了一个货币对象。然后,将另一个数值添加到、从、乘以、除以货币对象。然后,添加基于百分比的税、固定折扣和基于百分比的折扣。最后,根据计算结果删除税和折扣,并使用get()
方法格式化和检索货币对象。all()
方法返回货币和税额数组。getTax()
方法返回计算的税额。getDiscount()
方法返回计算的折扣额。
<?php use Ferdiunal\Money\Money; // Create a new Money instance with a value of 100.50 $money = Money::make(100.50); // Add 50.25 to the Money instance $money->sum(50.25); // Subtract 10.50 from the Money instance $money->subtract(10.50); // Multiply the Money instance by 2 $money->multiply(2); // Divide the Money instance by 3 $money->divide(3); // Add a 20% tax to the Money instance $money->addTax(20); // Add a fixed discount of 15 to the Money instance $money->addDiscount(15, true); // Add a 10% discount to the Money instance $money->addDiscount(10); // Remove the 20% tax from the Money instance $money->removeTax(20); // Enable locale usage and set the locale code to USD $money->setLocaleActive(true)->setLocaleCode('USD'); // Get the Money instance as a formatted string $formattedMoney = $money->get(); // Get the tax amount as a formatted string $taxAmount = $money->getTax(); // Get the discount amount as a formatted string $discountAmount = $money->getDiscount(); // Get the Money instance and tax amount as an array $allData = $money->all(); // Output the formatted string echo $formattedMoney; // $208.00 // Output the tax amount echo $taxAmount; // $31.20 // Output the discount amount echo $discountAmount; // $28.20 // Output the Money instance and tax amount as an array print_r($allData); // Array ( [money] => 179.80 [tax] => 31.20, [discount] => 28.20 )
贡献
请参阅contributing.md以获取详细信息和工作列表。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件而不是问题跟踪器联系作者。
鸣谢
许可
请参阅许可文件以获取更多信息。