{"id":37163320,"url":"https://github.com/rainhq/decimal","last_synced_at":"2026-01-14T19:25:23.269Z","repository":{"id":38187864,"uuid":"123320086","full_name":"rainhq/decimal","owner":"rainhq","description":"Arbitrary-precision fixed-point decimal numbers in go","archived":false,"fork":true,"pushed_at":"2025-01-02T18:17:36.000Z","size":152,"stargazers_count":0,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-08-15T03:01:13.122Z","etag":null,"topics":["backend"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"shopspring/decimal","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rainhq.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}},"created_at":"2018-02-28T17:43:04.000Z","updated_at":"2022-09-25T15:15:32.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/rainhq/decimal","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/rainhq/decimal","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainhq%2Fdecimal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainhq%2Fdecimal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainhq%2Fdecimal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainhq%2Fdecimal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rainhq","download_url":"https://codeload.github.com/rainhq/decimal/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rainhq%2Fdecimal/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28432592,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T18:57:19.464Z","status":"ssl_error","status_checked_at":"2026-01-14T18:52:48.501Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["backend"],"created_at":"2026-01-14T19:25:22.617Z","updated_at":"2026-01-14T19:25:23.261Z","avatar_url":"https://github.com/rainhq.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# decimal\n\n[![Build Status](https://travis-ci.org/shopspring/decimal.png?branch=master)](https://travis-ci.org/shopspring/decimal) [![GoDoc](https://godoc.org/github.com/shopspring/decimal?status.svg)](https://godoc.org/github.com/shopspring/decimal) [![Go Report Card](https://goreportcard.com/badge/github.com/shopspring/decimal)](https://goreportcard.com/report/github.com/shopspring/decimal)\n\nArbitrary-precision fixed-point decimal numbers in go.\n\nNOTE: can \"only\" represent numbers with a maximum of 2^31 digits after the decimal point.\n\n## Features\n\n * the zero-value is 0, and is safe to use without initialization\n * addition, subtraction, multiplication with no loss of precision\n * division with specified precision\n * database/sql serialization/deserialization\n * json and xml serialization/deserialization\n\n## Install\n\nRun `go get github.com/shopspring/decimal`\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/shopspring/decimal\"\n)\n\nfunc main() {\n\tprice, err := decimal.NewFromString(\"136.02\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tquantity := decimal.NewFromFloat(3)\n\n\tfee, _ := decimal.NewFromString(\".035\")\n\ttaxRate, _ := decimal.NewFromString(\".08875\")\n\n\tsubtotal := price.Mul(quantity)\n\n\tpreTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1)))\n\n\ttotal := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1)))\n\n\tfmt.Println(\"Subtotal:\", subtotal)                      // Subtotal: 408.06\n\tfmt.Println(\"Pre-tax:\", preTax)                         // Pre-tax: 422.3421\n\tfmt.Println(\"Taxes:\", total.Sub(preTax))                // Taxes: 37.482861375\n\tfmt.Println(\"Total:\", total)                            // Total: 459.824961375\n\tfmt.Println(\"Tax rate:\", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875\n}\n```\n\n## Documentation\n\nhttp://godoc.org/github.com/shopspring/decimal\n\n## Production Usage\n\n* [Spring](https://shopspring.com/), since August 14, 2014.\n* If you are using this in production, please let us know!\n\n## FAQ\n\n#### Why don't you just use float64?\n\nBecause float64s (or any binary floating point type, actually) can't represent\nnumbers such as 0.1 exactly.\n\nConsider this code: http://play.golang.org/p/TQBd4yJe6B You might expect that\nit prints out `10`, but it actually prints `9.999999999999831`. Over time,\nthese small errors can really add up!\n\n#### Why don't you just use big.Rat?\n\nbig.Rat is fine for representing rational numbers, but Decimal is better for\nrepresenting money. Why? Here's a (contrived) example:\n\nLet's say you use big.Rat, and you have two numbers, x and y, both\nrepresenting 1/3, and you have `z = 1 - x - y = 1/3`. If you print each one\nout, the string output has to stop somewhere (let's say it stops at 3 decimal\ndigits, for simplicity), so you'll get 0.333, 0.333, and 0.333. But where did\nthe other 0.001 go?\n\nHere's the above example as code: http://play.golang.org/p/lCZZs0w9KE\n\nWith Decimal, the strings being printed out represent the number exactly. So,\nif you have `x = y = 1/3` (with precision 3), they will actually be equal to\n0.333, and when you do `z = 1 - x - y`, `z` will be equal to .334. No money is\nunaccounted for!\n\nYou still have to be careful. If you want to split a number `N` 3 ways, you\ncan't just send `N/3` to three different people. You have to pick one to send\n`N - (2/3*N)` to. That person will receive the fraction of a penny remainder.\n\nBut, it is much easier to be careful with Decimal than with big.Rat.\n\n#### Why isn't the API similar to big.Int's?\n\nbig.Int's API is built to reduce the number of memory allocations for maximal\nperformance. This makes sense for its use-case, but the trade-off is that the\nAPI is awkward and easy to misuse.\n\nFor example, to add two big.Ints, you do: `z := new(big.Int).Add(x, y)`. A\ndeveloper unfamiliar with this API might try to do `z := a.Add(a, b)`. This\nmodifies `a` and sets `z` as an alias for `a`, which they might not expect. It\nalso modifies any other aliases to `a`.\n\nHere's an example of the subtle bugs you can introduce with big.Int's API:\nhttps://play.golang.org/p/x2R_78pa8r\n\nIn contrast, it's difficult to make such mistakes with decimal. Decimals\nbehave like other go numbers types: even though `a = b` will not deep copy\n`b` into `a`, it is impossible to modify a Decimal, since all Decimal methods\nreturn new Decimals and do not modify the originals. The downside is that\nthis causes extra allocations, so Decimal is less performant.  My assumption\nis that if you're using Decimals, you probably care more about correctness\nthan performance.\n\n## License\n\nThe MIT License (MIT)\n\nThis is a heavily modified fork of [fpd.Decimal](https://github.com/oguzbilgic/fpd), which was also released under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frainhq%2Fdecimal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frainhq%2Fdecimal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frainhq%2Fdecimal/lists"}