{"id":13704641,"url":"https://github.com/sebastianbergmann/money","last_synced_at":"2025-09-27T02:31:53.284Z","repository":{"id":2988920,"uuid":"4005255","full_name":"sebastianbergmann/money","owner":"sebastianbergmann","description":"Value Object that represents a monetary value (using a currency's smallest unit).","archived":true,"fork":false,"pushed_at":"2018-01-24T07:27:56.000Z","size":13358,"stargazers_count":734,"open_issues_count":1,"forks_count":332,"subscribers_count":29,"default_branch":"master","last_synced_at":"2024-09-21T10:02:50.419Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sebastianbergmann.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":null,"security":null,"support":null}},"created_at":"2012-04-12T13:58:51.000Z","updated_at":"2024-09-17T19:27:44.000Z","dependencies_parsed_at":"2022-08-30T23:01:37.453Z","dependency_job_id":null,"html_url":"https://github.com/sebastianbergmann/money","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastianbergmann%2Fmoney","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastianbergmann%2Fmoney/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastianbergmann%2Fmoney/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastianbergmann%2Fmoney/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebastianbergmann","download_url":"https://codeload.github.com/sebastianbergmann/money/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219871850,"owners_count":16554459,"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-08-02T21:01:14.552Z","updated_at":"2025-09-27T02:31:47.980Z","avatar_url":"https://github.com/sebastianbergmann.png","language":"PHP","funding_links":[],"categories":["E-commerce","电子商务","目录","电子商务 E-commerce","电子商务( E-commerce )"],"sub_categories":["电子商务 E-commerce"],"readme":"**This project has been abandoned.** It was only ever intended to be used as an example for PHPUnit features etc. and not for usage in production. I am sorry that I failed to make that clear. Please have a look at [moneyphp/money](https://github.com/moneyphp/money) instead.\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).\n\n## Installation\n\nSimply add a dependency on `sebastian/money` to your project's `composer.json` file if you use [Composer](http://getcomposer.org/) to manage the dependencies of your project.\n\nHere is a minimal example of a `composer.json` file that just defines a dependency on Money:\n\n    {\n        \"require\": {\n            \"sebastian/money\": \"^1.6\"\n        }\n    }\n\n## Usage Examples\n\n#### Creating a Money object and accessing its monetary value\n\n```php\nuse SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\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 SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\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#### Using a Currency-specific subclass of Money\n\n```php\nuse SebastianBergmann\\Money\\EUR;\n\n// Create Money object that represents 1 EUR\n$m = new EUR(100);\n\n// Access the Money object's monetary value\nprint $m-\u003egetAmount();\n```\n\nThe code above produces the output shown below:\n\n    100\n\nPlease note that there is no subclass of `Money` that is specific to Turkish Lira as `TRY` is not a valid class name in PHP.\n\n#### Formatting a Money object using PHP's built-in NumberFormatter\n\n```php\nuse SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\Money\\Money;\nuse SebastianBergmann\\Money\\IntlFormatter;\n\n// Create Money object that represents 1 EUR\n$m = new Money(100, new Currency('EUR'));\n\n// Format a Money object using PHP's built-in NumberFormatter (German locale)\n$f = new IntlFormatter('de_DE');\n\nprint $f-\u003eformat($m);\n```\n\nThe code above produces the output shown below:\n\n    1,00 €\n\n#### Basic arithmetic using Money objects\n\n```php\nuse SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\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#### Comparing Money objects\n\n```php\nuse SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\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\nvar_dump($a-\u003elessThan($b));\nvar_dump($a-\u003egreaterThan($b));\n\nvar_dump($b-\u003elessThan($a));\nvar_dump($b-\u003egreaterThan($a));\n\nvar_dump($a-\u003ecompareTo($b));\nvar_dump($a-\u003ecompareTo($a));\nvar_dump($b-\u003ecompareTo($a));\n```\n\nThe code above produces the output shown below:\n\n    bool(true)\n    bool(false)\n    bool(false)\n    bool(true)\n    int(-1)\n    int(0)\n    int(1)\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 SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\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 SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\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 SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\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 SebastianBergmann\\Money\\Currency;\nuse SebastianBergmann\\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastianbergmann%2Fmoney","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebastianbergmann%2Fmoney","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastianbergmann%2Fmoney/lists"}