{"id":13412852,"url":"https://github.com/rhymond/go-money","last_synced_at":"2025-05-13T16:07:28.093Z","repository":{"id":19445213,"uuid":"85599716","full_name":"Rhymond/go-money","owner":"Rhymond","description":"Go implementation of Fowler's Money pattern","archived":false,"fork":false,"pushed_at":"2025-03-13T05:04:45.000Z","size":144,"stargazers_count":1759,"open_issues_count":29,"forks_count":154,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-04-23T21:52:14.279Z","etag":null,"topics":["currency","formatter","fowler-money-pattern","money","money-library"],"latest_commit_sha":null,"homepage":"","language":"Go","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/Rhymond.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-20T16:23:54.000Z","updated_at":"2025-04-23T15:07:53.000Z","dependencies_parsed_at":"2023-11-14T17:25:38.160Z","dependency_job_id":"56e23a50-6184-4311-b250-2b9cc10d4579","html_url":"https://github.com/Rhymond/go-money","commit_stats":{"total_commits":125,"total_committers":38,"mean_commits":3.289473684210526,"dds":0.624,"last_synced_commit":"89bfd6b376f2e1c6047cd2141f39c1cc6a4d86c0"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rhymond%2Fgo-money","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rhymond%2Fgo-money/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rhymond%2Fgo-money/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rhymond%2Fgo-money/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rhymond","download_url":"https://codeload.github.com/Rhymond/go-money/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253979991,"owners_count":21994041,"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":["currency","formatter","fowler-money-pattern","money","money-library"],"created_at":"2024-07-30T20:01:30.079Z","updated_at":"2025-05-13T16:07:28.039Z","avatar_url":"https://github.com/Rhymond.png","language":"Go","funding_links":[],"categories":["金融","Financial","金融领域相关库","金融领域相关库`处理货币与金融领域的库`","\u003cspan id=\"金融-financial\"\u003e金融 Financial\u003c/span\u003e"],"sub_categories":["检索及分析资料库","Advanced Console UIs","Search and Analytic Databases","SQL 查询语句构建库","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"# Money\n\n![alt text](http://i.imgur.com/c3XmCC6.jpg \"Money\")\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/rhymond/go-money)](https://goreportcard.com/report/github.com/rhymond/go-money)\n[![Coverage Status](https://coveralls.io/repos/github/Rhymond/go-money/badge.svg?branch=master)](https://coveralls.io/github/Rhymond/go-money?branch=master)\n[![GoDoc](https://godoc.org/github.com/Rhymond/go-money?status.svg)](https://godoc.org/github.com/Rhymond/go-money)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n**GoMoney** provides ability to work with [monetary value using a currency's smallest unit](https://martinfowler.com/eaaCatalog/money.html).\nThis package provides basic and precise Money operations such as rounding, splitting and allocating.  Monetary values should not be stored as floats due to small rounding differences.\n\n```go\npackage main\n\nimport (\n  \"log\"\n\n  \"github.com/Rhymond/go-money\"\n)\n\nfunc main() {\n    pound := money.New(100, money.GBP)\n    twoPounds, err := pound.Add(pound)\n\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    parties, err := twoPounds.Split(3)\n\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    parties[0].Display() // £0.67\n    parties[1].Display() // £0.67\n    parties[2].Display() // £0.66\n}\n\n```\nQuick start\n-\nGet the package:\n\n``` bash\n$ go get github.com/Rhymond/go-money\n```\n\n## Features\n* Provides a Money struct which stores information about an Money amount value and its currency.\n* Provides a ```Money.Amount``` struct which encapsulates all information about a monetary unit.\n* Represents monetary values as integers, in cents. This avoids floating point rounding errors.\n* Represents currency as ```Money.Currency``` instances providing a high level of flexibility.\n\nUsage\n-\n### Initialization\nInitialize Money by using smallest unit value (e.g 100 represents 1 pound). Use ISO 4217 Currency Code to set money Currency. Note that constants are also provided for all ISO 4217 currency codes.\n```go\npound := money.New(100, money.GBP)\n```\nOr initialize Money using the direct amount.\n```go\nquarterEuro := money.NewFromFloat(0.25, money.EUR)\n```\nComparison\n-\n**Go-money** provides base compare operations like:\n\n* Equals\n* GreaterThan\n* GreaterThanOrEqual\n* LessThan\n* LessThanOrEqual\n* Compare\n\nComparisons must be made between the same currency units.\n\n```go\npound := money.New(100, money.GBP)\ntwoPounds := money.New(200, money.GBP)\ntwoEuros := money.New(200, money.EUR)\n\npound.GreaterThan(twoPounds) // false, nil\npound.LessThan(twoPounds) // true, nil\ntwoPounds.Equals(twoEuros) // false, error: Currencies don't match\ntwoPounds.Compare(pound) // 1, nil\npound.Compare(twoPounds) // -1, nil\npound.Compare(pound) // 0, nil\npound.Compare(twoEuros) // pound.amount, ErrCurrencyMismatch\n```\nAsserts\n-\n* IsZero\n* IsNegative\n* IsPositive\n\n#### Zero value\n\nTo assert if Money value is equal to zero use `IsZero()`\n\n```go\npound := money.New(100, money.GBP)\nresult := pound.IsZero() // false\n```\n\n#### Positive value\n\nTo assert if Money value is more than zero use `IsPositive()`\n\n```go\npound := money.New(100, money.GBP)\npound.IsPositive() // true\n```\n\n#### Negative value\n\nTo assert if Money value is less than zero use `IsNegative()`\n\n```go\npound := money.New(100, money.GBP)\npound.IsNegative() // false\n```\n\nOperations\n-\n* Add\n* Subtract\n* Multiply\n* Absolute\n* Negative\n\nComparisons must be made between the same currency units.\n\n#### Addition\n\nAdditions can be performed using `Add()`.\n\n```go\npound := money.New(100, money.GBP)\ntwoPounds := money.New(200, money.GBP)\n\nresult, err := pound.Add(twoPounds) // £3.00, nil\n```\n\n#### Subtraction\n\nSubtraction can be performed using `Subtract()`.\n\n```go\npound := money.New(100, money.GBP)\ntwoPounds := money.New(200, money.GBP)\n\nresult, err := pound.Subtract(twoPounds) // -£1.00, nil\n```\n\n#### Multiplication\n\nMultiplication can be performed using `Multiply()`.\n\n```go\npound := money.New(100, money.GBP)\n\nresult := pound.Multiply(2) // £2.00\n```\n\n#### Absolute\n\nReturn `absolute` value of Money structure\n\n```go\npound := money.New(-100, money.GBP)\n\nresult := pound.Absolute() // £1.00\n```\n\n#### Negative\n\nReturn `negative` value of Money structure\n\n```go\npound := money.New(100, money.GBP)\n\nresult := pound.Negative() // -£1.00\n```\n\nAllocation\n-\n\n* Split\n* Allocate\n\n#### Splitting\n\nIn order to split Money for parties without losing any pennies due to rounding differences, use `Split()`.\n\nAfter division leftover pennies will be distributed round-robin amongst the parties. This means that parties listed first will likely receive more pennies than ones that are listed later.\n\n```go\npound := money.New(100, money.GBP)\nparties, err := pound.Split(3)\n\nif err != nil {\n    log.Fatal(err)\n}\n\nparties[0].Display() // £0.34\nparties[1].Display() // £0.33\nparties[2].Display() // £0.33\n```\n\n#### Allocation\n\nTo perform allocation operation use `Allocate()`.\n\nIt splits money using the given ratios without losing pennies and as Split operations distributes leftover pennies amongst the parties with round-robin principle.\n\n```go\npound := money.New(100, money.GBP)\n// Allocate is variadic function which can receive ratios as\n// slice (int[]{33, 33, 33}...) or separated by a comma integers\nparties, err := pound.Allocate(33, 33, 33)\n\nif err != nil {\n    log.Fatal(err)\n}\n\nparties[0].Display() // £0.34\nparties[1].Display() // £0.33\nparties[2].Display() // £0.33\n```\n\nFormat\n-\n\nTo format and return Money as a string use `Display()`.\n\n```go\nmoney.New(123456789, money.EUR).Display() // €1,234,567.89\n```\nTo format and return Money as a float64 representing the amount value in the currency's subunit use `AsMajorUnits()`.\n\n```go\nmoney.New(123456789, money.EUR).AsMajorUnits() // 1234567.89\n```\n\nContributing\n-\nThank you for considering contributing!\nPlease use GitHub issues and Pull Requests for contributing.\n\nLicense\n-\nThe MIT License (MIT). Please see License File for more information.\n\n\n\n[![forthebadge](http://forthebadge.com/images/badges/built-with-love.svg)](https://github.com/Rhymond/go-money)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhymond%2Fgo-money","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frhymond%2Fgo-money","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frhymond%2Fgo-money/lists"}