moneyphp/money
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
MoneyPHP/Money is a PHP library that provides a practical and safe way to handle monetary values. Built around Martin Fowler's Money pattern, this library substitutes the potentially error-inducing method of storing currency as a float, and instead employs a value object - the Money. It uses strings internally to represent monetary values, supporting unlimited integers. This approach ensures that you can handle financial transactions in your PHP applications more accurately and dependably. The library also supports JSON serialization, big integer utilization, money formatting, including intl formatter, currency repositories including ISO currencies, and money exchange features which includes the Swap implementation.
To use MoneyPHP/Money in your application, you first need to install it via composer. Run the command composer require moneyphp/money
in your terminal within your project's directory. Once the library is installed, you can use it in your PHP code like in the example below:
<?php
use Money\Money;
$fiveEur = Money::EUR(500);
$tenEur = $fiveEur->add($fiveEur);
list($part1, $part2, $part3) = $tenEur->allocate([1, 1, 1]);
assert($part1->equals(Money::EUR(334)));
assert($part2->equals(Money::EUR(333)));
assert($part3->equals(Money::EUR(333)));
In this example, the Money value object is used to handle addition and allocation of monetary values. You need to initialize the object with a currency and an amount (here, β¬5 is represented as 500 cents to avoid floats). Then you can use the add
method to add monetary values. MoneyPHP also allows you to allocate money easily by dividing it into proportions without losing a single cent due to rounding errors.
The comprehensive documentation for the MoneyPHP/Money library is available on their official website. The documentation provides detailed usage guides and extensive examples covering all of the features, from basic money operations to advanced features like money formatting and currency conversions.