{"id":19944128,"url":"https://github.com/rebilly/money","last_synced_at":"2026-03-07T23:02:04.886Z","repository":{"id":33719215,"uuid":"160975568","full_name":"Rebilly/money","owner":"Rebilly","description":"Money value object","archived":false,"fork":false,"pushed_at":"2025-04-15T22:20:36.000Z","size":106,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":21,"default_branch":"main","last_synced_at":"2025-04-15T23:20:35.982Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Rebilly.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-12-08T20:22:28.000Z","updated_at":"2025-04-15T22:11:23.000Z","dependencies_parsed_at":"2025-04-15T23:29:59.494Z","dependency_job_id":null,"html_url":"https://github.com/Rebilly/money","commit_stats":{"total_commits":21,"total_committers":8,"mean_commits":2.625,"dds":0.6190476190476191,"last_synced_commit":"34253d348d29b4cd4e74539bf8024fb647ecc3c3"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rebilly%2Fmoney","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rebilly%2Fmoney/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rebilly%2Fmoney/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rebilly%2Fmoney/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rebilly","download_url":"https://codeload.github.com/Rebilly/money/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252216010,"owners_count":21713079,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T00:19:11.312Z","updated_at":"2026-03-07T23:02:04.807Z","avatar_url":"https://github.com/Rebilly.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Software License][ico-license]][link-license]\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![GitHub Actions status][ico-github-actions]][link-github]\n\n# Money\n\n[Value Object](http://martinfowler.com/bliki/ValueObject.html) that represents a [monetary value using a currency's smallest unit](http://martinfowler.com/eaaCatalog/money.html).  This library is originally based on Sebastian Bergmann's [discontinued library](https://github.com/sebastianbergmann/money).  It's been updated with support added for currency exchange.\n\n## Installation\n\nSimply add a dependency on `rebilly/money` to your project's `composer.json` file if you use [Composer](https://getcomposer.org/) to manage the dependencies of your project.\n\n## Usage Examples\n\n#### Creating a Money object and accessing its monetary value\n\n```php\nuse Money\\Currency;\nuse Money\\Money;\n\n// Create Money object that represents 1 EUR\n$m = new Money(100, new Currency('EUR'));\n\n// Access the Money object's monetary value\nprint $m-\u003egetAmount();\n\n// Access the Money object's monetary value converted to its base units\nprint $m-\u003egetConvertedAmount();\n```\n\nThe code above produces the output shown below:\n\n    100\n    \n    1.00\n\n#### Creating a Money object from a string value\n\n```php\nuse Money\\Currency;\nuse Money\\Money;\n\n// Create Money object that represents 12.34 EUR\n$m = Money::fromString('12.34', new Currency('EUR'))\n\n// Access the Money object's monetary value\nprint $m-\u003egetAmount();\n```\n\nThe code above produces the output shown below:\n\n    1234\n\n\n#### Basic arithmetic using Money objects\n\n```php\nuse Money\\Currency;\nuse Money\\Money;\n\n// Create two Money objects that represent 1 EUR and 2 EUR, respectively\n$a = new Money(100, new Currency('EUR'));\n$b = new Money(200, new Currency('EUR'));\n\n// Negate a Money object\n$c = $a-\u003enegate();\nprint $c-\u003egetAmount();\n\n// Calculate the sum of two Money objects\n$c = $a-\u003eadd($b);\nprint $c-\u003egetAmount();\n\n// Calculate the difference of two Money objects\n$c = $b-\u003esubtract($a);\nprint $c-\u003egetAmount();\n\n// Multiply a Money object with a factor\n$c = $a-\u003emultiply(2);\nprint $c-\u003egetAmount();\n```\n\nThe code above produces the output shown below:\n\n    -100\n    300\n    100\n    200\n\n\nThe `compareTo()` method returns an integer less than, equal to, or greater than\nzero if the value of one `Money` object is considered to be respectively less\nthan, equal to, or greater than that of another `Money` object.\n\nYou can use the `compareTo()` method to sort an array of `Money` objects using\nPHP's built-in sorting functions:\n\n```php\nuse Money\\Currency;\nuse Money\\Money;\n\n$m = array(\n    new Money(300, new Currency('EUR')),\n    new Money(100, new Currency('EUR')),\n    new Money(200, new Currency('EUR'))\n);\n\nusort(\n    $m,\n    function ($a, $b) { return $a-\u003ecompareTo($b); }\n);\n\nforeach ($m as $_m) {\n    print $_m-\u003egetAmount() . \"\\n\";\n}\n```\n\nThe code above produces the output shown below:\n\n    100\n    200\n    300\n\n#### Allocate the monetary value represented by a Money object among N targets\n\n```php\nuse Money\\Currency;\nuse Money\\Money;\n\n// Create a Money object that represents 0,99 EUR\n$a = new Money(99, new Currency('EUR'));\n\nforeach ($a-\u003eallocateToTargets(10) as $t) {\n    print $t-\u003egetAmount() . \"\\n\";\n}\n```\n\nThe code above produces the output shown below:\n\n    10\n    10\n    10\n    10\n    10\n    10\n    10\n    10\n    10\n    9\n\n#### Allocate the monetary value represented by a Money object using a list of ratios\n\n```php\nuse Money\\Currency;\nuse Money\\Money;\n\n// Create a Money object that represents 0,05 EUR\n$a = new Money(5, new Currency('EUR'));\n\nforeach ($a-\u003eallocateByRatios(array(3, 7)) as $t) {\n    print $t-\u003egetAmount() . \"\\n\";\n}\n```\n\nThe code above produces the output shown below:\n\n    2\n    3\n\n#### Extract a percentage (and a subtotal) from the monetary value represented by a Money object\n\n```php\nuse Money\\Currency;\nuse Money\\Money;\n\n// Create a Money object that represents 100,00 EUR\n$original = new Money(10000, new Currency('EUR'));\n\n// Extract 21% (and the corresponding subtotal)\n$extract = $original-\u003eextractPercentage(21);\n\nprintf(\n    \"%d = %d + %d\\n\",\n    $original-\u003egetAmount(),\n    $extract['subtotal']-\u003egetAmount(),\n    $extract['percentage']-\u003egetAmount()\n);\n```\n\nThe code above produces the output shown below:\n\n    10000 = 8265 + 1735\n\nPlease note that this extracts the percentage out of a monetary value where the\npercentage is already included. If you want to get the percentage of the\nmonetary value you should use multiplication (`multiply(0.21)`, for instance,\nto calculate 21% of a monetary value represented by a Money object) instead.\n\n#### Convert Money object currency to to another Money object given a conversion rate \n\n```php\nuse Money\\Currency;\nuse Money\\Money;\n\n// Create a Money object that represents 100,00 EUR\n$original = new Money(10000, new Currency('EUR'));\n\n$converted = $original-\u003econvert(new Currency('USD'), 1.09, PHP_ROUND_HALF_UP);\n$converted-\u003egetConvertedAmount(); // 109.00\n```\n\n#### Convert Money using a Rate Object\n\n```php\n$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));\n$rate = new Rate($cp, new DateTime(), 0.92);\n\n$money = new Money(1000, new Currency('USD'));\n$markupBips = 500; // 5% markup\n\n$newMoney = $rate-\u003econvert($money)-\u003emultiply($markupBips / 10000 + 1);\n```\n\n#### Get Currency Exchange Rate Using a Rate Provider\n\n```php\n$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));\n$rateProvider = new InMemoryRateProvider(['EUR/USD' =\u003e 1.09, 'USD/EUR' =\u003e 0.9172], new DateTime());\n$rate = $c-\u003egetRate($rateProvider);\n```\n\n## Tests\n\n```\nphpunit\n```\n\n## Security\n\nIf you discover a security vulnerability, please report it to security at rebilly dot com.\n\n## License\n\nThe Money library is open-sourced under the [MIT License](./LICENSE) distributed with the software. \n\n\n[ico-github-actions]: https://github.com/Rebilly/money/workflows/Tests/badge.svg\n[ico-version]: https://img.shields.io/packagist/v/Rebilly/money.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square\n\n[link-github]: https://github.com/Rebilly/money\n[link-packagist]: https://packagist.org/packages/Rebilly/money\n[link-license]: LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frebilly%2Fmoney","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frebilly%2Fmoney","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frebilly%2Fmoney/lists"}